i have main activity , 2 fragments on it, fragment1 call fragment2 when clicking button built in android buttons call onbackpressed function overrided in main activity , exit me app. want when click on fragment 2 onbackpressed of main activity not called , me fragment 1. button of action bar works (i think mean backstack ok).
i tried do:
getactivity().getsupportfragmentmanager().begintransaction().addtobackstack("messages"); on fragment 2
and tried add
getsupportfragmentmanager().popbackstack(); to onbackpressed() of main activity dosen't did nothing thank you
edit:
i trying public void onbackpressed() { // getsupportfragmentmanager().popbackstack(); android.support.v4.app.fragment fragment2 = getsupportfragmentmanager().findfragmentbytag(null); if (fragment2 != null && fragment2.isvisible()) { getsupportfragmentmanager().popbackstack(); return; } and before show
getsupportfragmentmanager().begintransaction().addtobackstack(null).commit(); tr.show(fragment2); but got null on findfragment(null)
edit: checked getsupportfragmentmanager().begintransaction().mtail.fragment , saw value fragment 1 want showed, when getsupportfragmentmanager().popbackstack(); dosen't nothing.
just use below way; in activity check this
@override public void onbackpressed() { // todo auto-generated method stub fragment fragment = getsupportfragmentmanager().findfragmentbytag("secondfragment"); if (fragment instanceof secondfragment) { // show first fragment }else{ // finish(); } } i have used findfragmentbytag, can use findfragmentbyid
i agree @minhtdh comment applicable if replace 1 on other
Comments
Post a Comment