I am stuck with situation for resuming my fragment when I am coming back from another fragment.
Scenario :
I have a Fragment A. Now I am opening multiple fragment with in Fragment A say : A1, A2, A3 ... using a frame layout.
Now I am initiating a new Fragment say Fragment B from one of A1/A2/A3 ...
I am performing some action over Fragment B and now when I Pop Out my Fragment B then I am not able to get onResume() of Fragment A
Just need to get onResume() while I get back from Fragment B
Any help over this needed!
Highly appreciated!
Thanks.
Launch fragment A1, A2, A3 with childfragment manager from fragment A and launch fragment B with main fragment manager.
For Example :
To perform any fragment operation we have two fragment manager, If you are performing any fragment operation within a fragment you should use getChildFragmentManager() inplace of getSupportFragmentManager().
Now here, to launch fragment A1, A2 and A3 you should use getChildFragmentManager() and When launching fragment B you should use getSupportFragmentManager(). So when you press back from fragment B you will get onResume callback in fragment A.
When you have added your fragment B, have you added the transaction to the backstack with this command: addToBackstack(null); by this way, you can restore the previous state when you press back on fragment B and return to fragment A.
Related
Fox example, I have activities A, B, C. Each of these activities is a container for fragments F1, F2, F3. Also, I have another fragment - F4. This fragment which can be opened from fragments F1, F2, F3. In addition, from fragment F4 I need to perform navigation to activities A B and C and also remove from back stack fragment F4. Now I close the F4 fragment for this and using the fragment result API I pass the result to the previous fragment, and from the previous fragment I perform the navigation to the activity I need.
But in this case, the previous fragment appears on the screen for a few seconds, but I would like it to look like I navigated from fragment F4.
Is it possible to avoid this behavior ?
Please, help me ?
I have an activity Aa with Fragment Fa . Using childfragmentmanager in Fa I open another Fragment Frag. Using Fragment Frag I am starting another activity Ba using startActivityforresult. My usecase is to not to go back to Fragment frag if I successfully start activity Ba. So what I am doing right now is removing Fragment frag in its onActivityResult using childfragment transaction and pop it from back stack.
But its not working properly some time when I go back from activity Ba , Fragment frag is showing up. What I am doing wrong and what is the right way?
I have several fragments that share the same container: Fragment A, Fragment B, Fragment C. I move through them with the .replace method and I always use .addToBackStack to keep them in the stack.
At the current moment, if Fragment C is displayed and I click the Back Button to go back to Fragment B the methods from onCreateView() of Fragment B onwards will be called, but not onAttach or onCreate. I would like to restart Fragment B completely after I click the Back Button so that these methods are also called. In other words, as if I had gone from Fragment B to a different Activity and then back to Fragment B.
I Have Activity A, Inside that activity there is one fragment called F1 and inside that fragment i have another fragment F2. From F2 I called startActivityForResult(/Activity B/) and then when finishing activity B. onActivityResult() of fragment F2 is getting called but i want same thing to be happened in F1.
What I want is Fragment F1 should get the event that Activity B is finished.
Any help on this is appreciated.
Hi You need to get the current fragment form Fragment manager in Activity's
onActivityResult() Method.
Just make a call to fragment's onActivityResult() method.
CustomFragment fragment = (CustomFragment ) getFragmentManager().findFragmentByTag(getFragmentManager().getLastBackStackEntry());
fragment.onActivityResult();
You can use this technique.
You are most likely calling StartActivityForResult from your F2 fragment. Since you don't want the result going back to F2, you need to call that method on fragment F1 instead.
It would be nice to see some code to see what you are actually doing.
From your question it seems that F2 is child fragment for F1.
If so you can call startActivityForResult method from fragment F2 as below :
getParentFragment().startActivityForResult(intent, requestCode);
Now, when you will finish the second activity, it will automatically call onActivityResult() method of parent fragment (i.e. Fragment F1).
Android's Fragment's onResume/onPause methods are tightly coupled with the host Activity's lifecycle as shown here.
What I want to know is how to detect that a fragment is left from / returned to inside the app's navigation flow.
Example:
Say I have MainActivity and fragments A,B and C.
MainActivity adds fragment A, then B and then C.
How do I know fragment B was left (I now see fragment C).
Also, once I press on back, how do I know fragment B was resumed?
Edit:
Clarification: I want to know that from within fragment B (similar to the way an Activity works with onPause and onResume)
Try isDetached() method link here
Respectively there is isAdded()
Indirectly you question concern with Fragment LifeCycle.
And you can trace fragment navigation replace,remove,add using Fragment manager
PopBackStack : Pop the last fragment transition from the manager's fragment back stack. If there is nothing to pop, false is returned.