6

i have Fragment Activity With contatin navigation drawer.after clicking navigation drawer item it open a Fragment 1, fragment 2, Fragment 3.and then after button click event in Fragment 1 open new Fragment 4.

i want handle back key press event how can i do this on Fragment 4?after clicking back key in Fragment 4 i want to go back Fragment 1.

enter image description here

hash
  • 5,336
  • 7
  • 36
  • 59

3 Answers3

20

Add Fragments To BackStack. Before commit() the transaction, use addToBackStack() method i.e

 addToBackStack("Some String").commit();

and in onBackPressed()

@Override 
public void onBackPressed() { 
    if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
        getSupportFragmentManager().popBackStack(); 
    } else { 
        this.finish(); 
    }
}
hash
  • 5,336
  • 7
  • 36
  • 59
sandeepmaaram
  • 4,191
  • 2
  • 37
  • 42
  • you mean like this `getFragmentManager() .beginTransaction() .replace(R.id.content_frame, Entry_Account.newInstance(), Entry_Account.TAG).addToBackStack() .commit();` inside my `Fragment 1` but it says `The method addToBackStack(String) in the type FragmentTransaction is not applicable for the arguments ()` – hash Dec 31 '14 at 06:39
  • 5
    In Activity onBackPressed() method if(fm.getBackStackEntryCount()>0){pop the fragment} else{super.onBackPressed();} – sandeepmaaram Dec 31 '14 at 06:46
  • what do you mean in `{pop the fragment}` – hash Dec 31 '14 at 06:48
  • remove fragment from stack like fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); – sandeepmaaram Dec 31 '14 at 06:50
  • 1
    This is the correct answer. Adding fragment transactions to the backstack will reverse the transaction on back pressed. – DeeV Dec 31 '14 at 06:55
  • Fragments don't get added to the backstack. The transaction does. Everything you do in a transaction (add, replace, remove, hide, etc.) will be reversed all at once if you push it on the stack and then pressed "back". – DeeV Dec 31 '14 at 06:56
  • @Sameera Is it working for you? Yeah agree with above comment – sandeepmaaram Dec 31 '14 at 06:57
  • i add this on your code's `{pop the fragment}` section `getSupportFragmentManager().popBackStack();` – hash Dec 31 '14 at 06:58
  • `@Override public void onBackPressed() { if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportFragmentManager().popBackStack(); } else { this.finish(); } }` – hash Dec 31 '14 at 06:59
  • 1
    `addToBackStack("Some string") .commit();` – hash Dec 31 '14 at 09:07
  • 1
    I think else { super.onBackPressed(); } would provide a more graceful fallback than assuming Activity.finish(). Also, it's strange you need to implement this logic when using android.app.FragmentManager, but _not_ android.support.v4.app.FragmentManager. I received default back stack handling when using the support library version. – Damien Diehl Jul 01 '15 at 15:12
  • @damien5314 Oh my god this^ I just spent close to 2 hours trying to figure out what the hell had happened to my back stack and I had swapped from support fragments to normal ones recently. God dammit! – Daniel Wilson Nov 04 '15 at 14:51
5

Your code for adding fragment to backstack should be like:

getFragmentManager() .beginTransaction() .replace(R.id.content_frame, Entry_Account.newInstance(), Entry_Account.TAG).addToBackStack("Some string") .commit();

Then On Activity's onBackPressed method,use this snippet:

@Override
public void onBackPressed () {
if(getFramentManager().getBackStackEntryCount()>0){
      // popback statck.
}
 else{
     // finish your activity.
 }
}
Bhawna Raheja
  • 524
  • 6
  • 9
  • `@Override public void onBackPressed() { if (getSupportFragmentManager().getBackStackEntryCount() > 0) { getSupportFragmentManager().popBackStack(); } else { this.finish(); } }` – hash Dec 31 '14 at 07:04
4

Override onBackPress() method and check that your fragement manager has backStack entry>0 if it has greater than 0 than do popback stack otherwise finish your activity..

if(getFramentManager().getBackStackEntryCount()>0){
      // popback statck.
}
 else{
     // finish your activity.
 }

don't forget to add your fragement to BackStack to maintain fragment hierarchy..

fragTransacion.addToBackStack(TAG);
Sanket Kachhela
  • 10,861
  • 8
  • 50
  • 75