android - handle back button in single fragment -
i have application navigation drawer. when start application, have on main screen (screen a) "hello world" , when select item navigation drawer, load fragment , "new hello world" , again when select item navigation drawer, load fragment , "hello universe". since happening via 1 single fragment, when press device button should previous fragment below:
"hello universe" >press back> "new hello world" >press back> "hello world"
how handle ?
note:
while changing fragment have tried
fragmentmanager.begintransaction().replace(r.id.maincontent, fragment).commit();
then changed :
fragmentmanager.begintransaction().add(r.id.maincontent, fragment).addtobackstack("tag").commit();
but nothing worked. app exits on button press. due same fragment getting replaced content again , again?
the second approach tried correct.try call popbackstack()
on backpressed()
overriding onbackpressed()
method.
@override public void onbackpressed() { if (getfragmentmanager().getbackstackentrycount() > 0) { getfragmentmanager().popbackstack(); } else if (getfragmentmanager().getbackstackentrycount() == 0) { super.onbackpressed(); } }
Comments
Post a Comment