0

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.

nikhilvit
  • 7
  • 2
  • Sounds like you should just make sure to add the transaction to the backstack, but without any code it's a little hard to tell what you're currently doing. That is, what *"replaced over it"* really means. – MH. Oct 18 '14 at 13:26
  • dont forget to accept the answer that solved your problem – Elltz Oct 19 '14 at 14:51

2 Answers2

0

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

Community
  • 1
  • 1
vkm
  • 548
  • 7
  • 23
0
        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();
Elltz
  • 10,730
  • 4
  • 31
  • 59
  • Ok it works .Thanks. how do i get left arrow in nav bar left in 2nd fragment , for navigation . – nikhilvit Oct 18 '14 at 14:35
  • Use activity.actionbar.setDisplayHomeAsUpEnabled(true); when you have launched second fragment. – Harsha Vardhan Oct 18 '14 at 14:50
  • this is the [link](https://developer.android.com/training/basics/actionbar/adding-buttons.html) based on @HarshaVardhan answer – Elltz Oct 18 '14 at 14:55