简体   繁体   中英

Implementing back navigation in action bar in nested fragments with navigation drawer?

I have a navigation drawer as main activity.On click of one of its item , a fragment is added(as in this item its a list with custom list adap. ).[Fragment 1] Now on click on one of those items another fragment is replaced over it and it displays the details.[Fragment 2]

Now when i click back button , application is destroyed . I need to go on 1 st fragment on back button clicked.How do i do that . Thanks.

Fragments are a part of an Activity. onBackPressed() works differenly for fragments. You must be applying onBackPressed() on the Activity. Check here for how to do it in fragments : Using onBackPressed() in Android Fragments

        Fragment f2= new Fragment2();
        FragmentTransaction transaction = getFragmentManager().beginTransaction();
        transaction.add(R.id.fragment_container, f2);
        transaction.addToBackStack(null);//null is optional you could replace with string-
        transaction.commit();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM