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).
Related
I have fragment A, which calls fragment B with setTargetFragment.
Once fragment B finishes certain operations, it calls getTargetFragment().onActivityResult().
It works fine(meaning, fragment A's callback onActivityResult is being invoked as it should) , but fragment B is not detached or removed. The user still stays on fragment B for some reason. How is that possible? shouldn't oActivityResult remove the fragment from the stack, or at least go away for the user to see fragment A again ?
The process is pretty much the same as in an activity which you need to call finish() after setting the result. In case of a fragment you should call popBackStack() to remove the current fragment, the same way you call finish() to pop the current activity.
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.
I have A activity. This contain 4 Fragments. I created separated Fragment class in other packages. so A activity contain 4 Fragments. one of them is B fragment. Now B Fragment calls to Activity C by startActivityForResult. so i writes the method onActivityResult in B Fragment. But same method is also in Activity A which is most important and can't be remove. Now when I came back from C -- > B then onActivityResult is not calling but activity A's onActivityResult calls. so how can i call to this method in (B)fragment too. so i can change the data by calling it.
Try to call
super.onActivityResult();
in your Activities onActivityResult() to pass the Callback to your Fragment.
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.
Am Replacing Fragment B on top of Fragment A. Then Some time Later am calling popBackStack();. It's Pop the current fragment B from stack. Then Fragment A's OncreateView is getting called. I am not sure When the Fragment A get's detached from an activity. why it's calling fragment onCreateView again. For my case I want to call the Framgent A's OnStart Method instead of calling onCreateView method.
Please help me on this. Thanks in Advance.