Skip to content Skip to sidebar Skip to footer

How To Know If All Fragments Were Detached?

On my app I am using a Main Activity, which has a Navigation Drawer, and as the user go to an options from the drawer, it will change the fragment beging displayed accordingly to t

Solution 1:

When you push your first fragment, add a tag to it. Something like

transaction.replace( R.id.rootContainer, firstFragment, "rootFragment");

Whenever user presses back button, you can get your rootFragment from FragmentManager

FirstFragmentmyFragment= (FirstFragment) getSupportFragmentManager().findFragmentByTag("rootFragment");

And check if this fragment is visible or not, by myFragment.isVisible(), if not, then keep popping the stack, if it is visible, it means user is on the first fragment. Now you can either quit the app, or show your menu drawer.

Solution 2:

Good night good sir. Thank you for your tip. I used a different approach, based on your repply, that gave me quite a few insights.

On main activity, I Overrided the onKeyDown and check if it was pressed the Back Button. If yes, then I check the size of my Back Stack.

Uppon that, I decide if I want to finish my application.

Also, thanks for the tip of "labeling" the fragments on the back stack, didn't know I could do that.

Post a Comment for "How To Know If All Fragments Were Detached?"