android - What's happening to my Fragments? -
i know there tons of questions fragments , read many, seems haven't grasped yet.
my app has 9 fragments. on 1 of them, fragment x, playing media , there shows fragment on screen, y. perform that, thought best using replace()
.
my relevant code:
// button clicked, goes x y getfragmentmanager().begintransaction() .replace(r.id.container, fragmenty) .commit();
when onbackpressed()
called , fragmenty 1 returned fragmentmanager.findfragmentbyid(r.id.container)
:
fragmentmanager.begintransaction() .replace(r.id.container, fragmentx) .commit();
expected behavior: it'd remove x , add y. when going back, it'd remove y , add x.
what happened: first time go y, y shown expected, x still running hear audio playing. when go back, y disappears , seems fine. however, next time click go y, x destroyed (audio stops playing) , app gets weird, since actionbar shows , shouldn't. moreover, keep hidden fragment map gets blank after executing that.
i imagine replace() not removes current fragment, of visible fragments. doing .remove(x).add(y, "y") not show fragment.
any help?
edit: asked, fragment x can added activity different points on app, hence on different ways. they're two:
// if backpressed , current fragment fragmenttransaction.remove(fragmenta).add(r.id.container, objectfragment).commit();
or
// if backpressed , current fragment b (map) fragmenttransaction.hide(fragmentb).add(r.id.container, objectfragment).commit();
most of times must know coming , i'm going, that's why did not use addtobackstack()
.
i try manage adding fragment stack, using
addtobackstack(string name) method.
there helpful answer on subject here: difference between add(), replace(), , addtobackstack()
Comments
Post a Comment