android - addToBackStack doesn't work, closes the activity instead of popping back fragments -


i have probleam , can't find solution anywhere.

my app doesn't return previous fragment when press button, instead closes activity.

i have activity displays 4 fragment: [1],[2],[3],[4]. can switch between first 3 fragments action bar, don't want add them stack.

fragment [4] detailed view of item selected in fragment [3]. when press in [4] want return fragment [3], not closing app.

the transitions done in way activity in fragments placed:

private void replacefragment(fragment fragment, boolean tobackstack){      if(fragment != null){          fragmenttransaction fragmenttransaction = getfragmentmanager().begintransaction();         fragmenttransaction.replace(r.id.container, fragment);          if(tobackstack)             fragmenttransaction.addtobackstack(null);          fragmenttransaction.commit();     } } 

where tobackstack false except when transition [3] [4].

if pass tobackstack true in every transition, activity closes anyway.

i had same problem. specifically, implementing preferencefragment , want button return me whatever loaded fragment was.

it appears "back stack" not automatically triggered system button. solution manually pop stack onbackpressed override:

@override public void onbackpressed() {     if (insettings)     {         backfromsettingsfragment();         return;     }     super.onbackpressed(); } 

whenever navigate preferences fragment, set insettings boolean true in activity retain state. here backfromsettingsfragment method looks like:

private void backfromsettingsfragment() {     insettings = false;     getfragmentmanager().popbackstack(); } 

so, if able track state of when in fragment [4] , intercept button, should able manually call

getfragmentmanager().popbackstack(); 

to go fragment [3].

note: remember need add fragment [3] stack, not fragment[4]. (unless [4] goes [5] , need [4] well.)


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

keyboard - C++ GetAsyncKeyState alternative -

android - java.net.UnknownHostException(Unable to resolve host “URL”: No address associated with hostname) -