Alfred
Alfred

Reputation: 43

Navigating back to a viewpager fragment in a nested fragment

I have a ViewPagerContainer fragment that loads when the app starts. The ViewPagerContainer fragment adds two tabs(tab A and tab B) to the actionbar. Tab B has a listView of two items.

What I have done: I attached a click listener to a list view item in tab B fragment such that When a user clicks on an item it opens another fragment (child fragment) inside the first fragment i.e under tab B.

Where I got stuck: I'd like to go back to tab B when a user presses the back button. I tried implementing this by adding the child fragment to backstack, but unfortunately it does not go back to tab B fragment. Kindly help me out with this I have done alot of research on this including implementing backstack hacks with no success.

I am using v4 and v7 appcompat libraries.

Any help would me much appreciated. Thanks in advance.

EDITED: I managed to navigate back from the child fragment to actionbar tabs. I achieved this through the following code:

//used getChildFragmentManager() instead of getSupportFragmentManager() FragmentManager fm = getChildFragmentManager();

MyFragmentPagerAdapter fragAdapter = new MyFragmentPagerAdapter(fm);

viewPager.setAdapter(fragAdapter);

However, on back navigation the number of tabs double and only two of them are clickable and I can see their content. When I click on the listItem on the second tab again then navigate back, two more tabs are added and it goes on and on. How can I solve this? Here is the code

Tab B Fragment: public class FindStylist extends ListFragment {

String[] selectCat = {
        "category A",
        "Category B"
};

private ListView listView;
String mTime;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

ArrayAdapter myAdapter = new ArrayAdapter(inflater.getContext(), android.R.layout .simple_list_item_1, selectCat){

        public View getView(int position, View convertView, ViewGroup parent){

            View view = super.getView(position, convertView, parent);

            TextView tv = (TextView) view.findViewById(android.R.id.text1);

            tv.setTextColor(Color.BLACK);
            tv.setGravity(Gravity.CENTER);

            return view;

            //listView = view.getListView();

        }
    };

    setListAdapter(myAdapter);

return super.onCreateView(inflater, container, savedInstanceState);

}

public void onViewCreated(View view, Bundle savedInstanceState){

     listView = getListView();
            listView.setOnItemClickListener(new OnItemClickListener(){
                public void onItemClick(AdapterView<?> parent, View view, int position, long id){
                    if(position == 0){
                        //best clicked
                        Toast.makeText(getActivity(), " catA clicked " + position, 5).show();

                    }else if(position == 1){
                        //nearest clicked
                        Toast.makeText(getActivity(), "CatB clicked " + position, 5).show();

                        FragmentNearest fragment = new FragmentNearest();
                        getActivity().getSupportFragmentManager().beginTransaction()
                        .replace(R.id.mainContent, fragment)
                        .addToBackStack(null)
                        .commit();
                    }

                }
            });
}

}

Child fragment of B:

public class FragmentNearest extends ListFragment{

String[] selectLocation = {"From Current Location", "Choose a Location"};

private ListView listView;

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

ArrayAdapter myAdapter = new ArrayAdapter(inflater.getContext(), android.R.layout .simple_list_item_1, selectLocation){

        public View getView(int position, View convertView, ViewGroup parent){

            View view = super.getView(position, convertView, parent);

            TextView tv = (TextView) view.findViewById(android.R.id.text1);

            tv.setTextColor(Color.BLACK);
            tv.setGravity(Gravity.CENTER);

            return view;

        }
    };

    setListAdapter(myAdapter);

    return super.onCreateView(inflater, container, savedInstanceState);

}

Upvotes: 0

Views: 729

Answers (1)

Sebastian Pakieła
Sebastian Pakieła

Reputation: 3029

You should override onBackPressed method in activity class like this.

    @Override
    public void onBackPressed()
    {
        if (iCanBackToFragment)
        {
            popBackStackOrWhateverYouUseToBack()
        }
        else
        {
            super.onBackPressed();
        }
    }

Upvotes: 0

Related Questions