Resume Fragments in Android - android

I am currently working on sample android navigation drawer application. It contains 1 activity and four fragments (import android.app.Fragment). I do not know how to resume the previous fragments, for example now I am in Fragment A here I did some activities like selected checkbox some thing like this. then I have moved to Fragment B, now if I press back button I want to resume the Fragment A with the previous state (with selected check box). please provide some sample code.
thanks in advance

As described in the documentation (http://developer.android.com/training/implementing-navigation/temporal.html#back-fragments) you can add the fragment to the back stack:
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
// Add this transaction to the back stack
.addToBackStack()
.commit();

Related

Android: how to create a custom back button for fragments in a KIOSK app

I CANT use ActionToolbar because this is a KIOSK app with a custom header fragment that needs to be displayed at all time. Hence my situation, I have a basic ImageButton on the said header fragment, and I want to implement the functionality of a basic ActionToolbar.
I add my fragment to the main container like this:
getSupportFragmentManager().beginTransaction()
.replace(R.id.main_container, UserListFragment.newInstance())
.addToBackStack("foo")
.commit();
The question is, how do I restore the state of my main_container before the current fragment was replaced with UserListFragment.
Like : fragment A is showing, replacing fragment A with B, pressing some button in my header fragment to pop the fragment B so again fragment A is showing.
You should consider switching to Android Jetpack Navigation Component it was created for your type of situation.
Check: https://stackoverflow.com/a/62252603/8714139
Cheers!

Close the currrent fragment and open the previous one

Imagine i have a frameLayout in my main activity, and two fragments,
so now first fragment is being displayed, i replaced it with secondFragment and added first one to back stack, now some condition is met in the secondfragment so i want to close the second fragment and open the first one where we left that (basically back from the back stack) without pressing the back button. how do i achieve this ?
you can write this code for removing current fragment from backstack:
getActivity().getSupportFragmentManager().popBackStack();

Android Fragments switch without destroy

I have an activity with 3 fragments. Every fragment has a question.
When i create the activity I'm calling .add(...) method for the first fragment. For the others i call .replace(...) method with addToBackStack(...) method.
So i have this situation in the backstack:
.add(Frag1)
--> Frag 1
.replace(..., Frag2, ...)
-->Frag2
-->Frag1
.replace(..., Frag3, ...)
--> Frag3
--> Frag2
--> Frag1
Now, when I'm at the third fragment and then go back I'm popping it from the stack. I want to preserve the third fragment so if i go back to the second and then go next to the third i want to have the question answered and not to call again another onCreate().
I want to create a sort of switch of the fragments. Is it possible? Can someone help me?
You can create a fragment once and Show/Hide it depending on your needs.
See hide & show from FragmentTransaction docs
Also see this question
How about you use viewpager instead, you will need to:
Disable paging by swipe, so you can change the page programmatically
Load all pages at once (set setOffscreenPageLimit your pages count), so that each page keeps its state

Remove specific fragment from backstack

I have 2 fragments(Frag A, Frag B) which are shown in multi pane in landscape with different container id (R.id.containerA,R.id.containerB) respectively.
My screen flow for 1st fragment is FragA->FragA1->FragA2
My screen flow for 2nd fragment is FragB->FragB1
I am adding each fragment to backstack. So I have around 5 fragments in backstack.
Actual order of navigating is FragA->FragB->FragA1->FragB1->FragA2
Now when I press back button, I want FragB1 to be popped out first from backstack instead of FragA2. I know fragments are maintained in a stack but how to handle this particular multi pane scenario ?
Should I use reflection like mentioned in this post ?
Android Reorder Fragment Backstack
Any other alternatives ?
If you simply want to get FragB1 instead of FragA2, you can check by obtaining the name of the fragment and then go one step back if name of the fragment is FragA2. You can get the name using the following code:
FragmentManager.BackStackEntry backEntry=getFragmentManager().getBackStackEntryAt(getActivity().getFragmentManager().getBackStackEntryCount()-1);
String str=backEntry.getName();

Proper top level navigations in android fragments like in Google Play

I have an Activity with navigation drawer and a default fragment set in to Activity when application starts.
I have 4 top level navigation in my navigation drawer
Fragment 1
Fragment 2
Fragment 3
Fragment 4
and switching the fragments inside the activity on click on each each navigation. I want implement the fragment navigation in such manner that from each top level navigation fragment, if user clicks back button it should first come to Main or default fragment and from there app should exit same like in Google Play. I call it master fragment.
eg:
Default(master) Fragment > Fragment 1
Fragment > Fragment 2
Fragment 2 -- Back pressed > Deafult fragment and like so.
What I have tried so far :
I have tried adding fragment in backstack but it doesn't help it takes me all fragment in stack.
getSupportFragmentManager().beginTransaction()
.add(R.id.container, selectedFragment)
.addToBackStack("naviagtion_stack")
.commit();
My each top fragments also have child fragments in stack so stack count also did not help me.
I don't want to remove and add my default fragment because as it fetches some data from network so recreation will make the network call again which i don't want .
I want exactly what Google Play does. I just want to know the logic.
Add your master fragment to backstack and remember the tag: fragmentManager.beginTransaction()
.add(R.id.main_layout, masterFragment)
.addToBackStack(INITIAL_STATE)
.commit();
Click on the navigation elements should do following before adding a corresponding fragment: fragmentManager.popBackStack(INITIAL_STATE, 0);
This call removes from backstack everything but your master fragment.
All fragment transactions (including navigation fragments) should generally do the same thing, for example:
fragmentManager.beginTransaction()
.add(R.id.main_layout, fragment)
.addToBackStack(null)
.commit();

Categories

Resources