Remove specific fragment from backstack - android

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();

Related

How to prevent fragment recreating when conditionally navigating to previous fragment

I have two fragments A and B.
A is a form which contains EditTexts and one value need to be selected from fragment B.
When using navigation component after selecting item from B and when conditionally navigating to A , EditTexts values are gone. The fragment is recreated. How to prevent this?
My intention is to get value from B to A without recreating A
Without seeing your code its difficult to give you a straight answer, but here are some general ideas
1) Add fragment A to the backstack this will persist it when you return from Fragment B
2) Find fragment A by its tag and if it exists do not create a new Fragment A when returning to it

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

Clear backstack and replace current fragment with a top-level fragment

I have one activity in my android app which handles the replacement of multiple views/fragments. This activity also has a navigation drawer to navigate to top-level fragments. From the top-level fragments you can navigate to detail-level fragments.
The navigation drawer can always be accessed by sliding it in from the left side. If the current fragment is a top-level fragment, the navigation-drawer can also be accessed by the action-bar. If the current fragment is a detail-level fragment, you can navigate back through the action-bar (or by pressing the back-button).
Lets say I have 3 top-level fragments which can be accessed through the navigation drawer.
fragment[1], fragment[2], fragment[3]
From fragment[1], you can navigate to the detail-level fragment[1.1].
Now the user wants to navigate directly to the top-level fragment[3] from the current fragment[1.1]. The user just has to slide in the navigation-drawer and click the item for fragment[3].
Now, if the user hits the back button, the application should close (because it navigated to a top-level fragment). So every time the user navigates to a top-level fragment, the backstack should be cleared. To check if I should display the drawer indicator, I am reading the getBackStackEntryCount()-value and compare it with 0.
As a summary. I want to navigate from detail-level fragments to any top-level fragments and clear the backstack.
The problem:
When I clear the backstack by executing
getSupportFragmentManager().popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);,
the fragment transactions are replayed in reverse (for example the replacement of fragment[1] by fragment[1.1]). And I DON'T want them to be replayed. I have a lot of initializations in onCreateView, onViewCreated, onStart, .., also starting tasks to fetch data. And I want to skip that all. Just replace the detail level view with a new top-level one which wasn't already in the backstack.
I couldn't find any solution to clear the backstack without popping transactions.
Is there a clean solution to handle this problem? Do I have to implement my own backstack-behavior?
EDIT:
The YouTube app also behaves like this. When you navigate from detail to top-level, the backstack gets cleared. But how do they clear the backstack without replaying the fragment-transactions?
Thanks for your answers.
I think you problem is that you are popping fragments in a loop - I guess. I usually push fragments on the fragment stack together with specifying custom animations - say slide in new fragment from the right and when popping it our slid out to the right. Now - if your user has navigated to fragment 1 then 1.1 then 1.2 etc and you want to navigate back to 1 when a user clicks the home button in the action bar - specify a name for you fragment 1 transaction. The you you perform popping use that name. Also consider using popBackStack function instead of popBackStackImmediate.

Hold Fragment View State in Sliding menu library on deattach and then attach in transaction

I am trying to create an android application that uses sliding menu library.
Now my question is that suppose I have three fragment A, B, C and I load Fragment A initially then B then after C now when I attach Fragment A after detaching current viewing fragment, Fragment A starts from onCreateView and my view again generated.
So is there any way to store my view or save it so it does not generate again like in facebook which shows home screen initially then if you go to any menu option and press back then you again will come to home screen and event internet is not available it whole view is like it was before as what I think does not recreate.
transaction.hide(currentFragment);
transaction.show(detailScreen);
transaction.addToBackStack(null);
transaction.commit();
By this way i can hold my transaction without detaching it.and it will contains all information of a fragment.

Android SDK Fragments -- retain State over multiple activities

I am developing an application where i have a main activity consisting on the left of a fragment showing products. Depending on the selection, the right part of the landscape-view shows detailed information, where the product's properties can be thoroughly specified. This is the case if the application's running in landscape-mode. If the orientation changes to portrait-mode, then the user first selects a product and then specifies the product's properties by individual dedicated activities, where each activity contains exactly one fragment of the previously mentioned landscape-view.
The problem now comes when I rotate my screen during runtime. Assume that I started the application in portrait mode, and I select the first product (Activity A, Fragment A). The next screen (separate Activity, call it Activity B) will contain a Fragment B where I can specify Property 1 of the product. Now I rotate the screen. The application will then show the landscape-view where the first product is selected, and ALL fragments dedicated to product configuration are shown at the right. Is there a solution that I can retain the configurations done by the user in the separate activity (i.e. Fragment B, Activity B) in such a manner that the land-scape view receives the state such that the details view can properly show the hitherto done configurations?
To summarize:
I am in Activity A, just containing a list of products (Fragment A)
I select the first
then I get to a new Activity B, where Fragment B is shown
then I rotate my screen
then I get back to Activity A, now containing at the left side Fragment A (the list) and at the right side (among others) Fragment B.
I want the state of Fragment B in Activity B be available in Activity A, Fragment B.
Hope I could completely specify my question.
TIA
Gerald
There are 2 solutions that I could think of right now:
Forget about 2 Activities in this case, and solve this scenario with only 1. I had multiple projects with exactly these requirements. In portrait mode, I used a ViewSwitcher or a ViewFlipper to show Fragment A or Fragment B full-screen. On landscape, things remain the same you described. This way the 2 fragments can communicate with each other, Fragment B may update the selection on Fragment A.
Start Activity B with .startActivityForResult(), so it may supply a result for Activity A, containing the current selection.

Categories

Resources