1

I have 2 activities in my application:

1.Launch_activity which displays some details

2.Home_activity which contain several fragments say A1,A2,A3 listed as cards

My question is : How to return to a previous fragment. Example : If I'm in fragment A2, when I press back it should return to A1 and when I press back from A1, it should go to the Home_activity where all my fragments are shown as cards. Presently it is directly going to the Launch_activity when I press the back button. As I'm new here, could you please explain me how to solve this?

  • When you change your fragments, simply add the `addToBackStack(String)` – NSimon Jun 01 '16 at 13:21
  • @NicolasSimon Whats do you mean by 'When you change your fragments' ? Could you please explain I'm a beginner here . Thanks :) –  Jun 01 '16 at 13:36
  • See @Masum answer below. He changes fragments (like you're probably doing) with beginTransaction(). He then adds the `addToBackStack` call, so the system "pops this fragment out" whenever the user clicks on the back button – NSimon Jun 01 '16 at 13:37

1 Answers1

1

Your all fragment commit with add() method instead of replace() method. Something like this

android.app.Fragment fragment = new YourFragment();
FragmentManager frgManager = getFragmentManager();
android.app.FragmentTransaction ft = frgManager.beginTransaction();
ft.addToBackStack(null);
ft.add(R.id.content_frame, fragment);
ft.commit();
Masum
  • 4,879
  • 2
  • 23
  • 28
  • 1
    Thanks. It solved the issue. But it doesn't work for the navigation drawer. When I click back when I'm in navigation drawer It goes back to the Launch_activity. –  Jun 01 '16 at 14:03