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?
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 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).
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 small layout in my activity that I add Fragments to based on the User navigating through the app.
Assuming the user navigates thusly:
Activity -> Fragment A -> Fragment B -> Fragment C -> Button Click
I would like to be able to to hide the Fragments and show the blank Activity again.
This is how I'm adding the Fragments to the activity:
protected void addFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.secondary_fragment, fragment).addToBackStack(fragment.getTitle()).commit();
}
To clear all the Fragments, I use:
getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
However, is there a way to clear the fragments in a way that if the user presses back, they would be able to go back to Fragment C (as opposed to exiting the App)?
Maybe instead of pop all the backStack, you just get the fragment view by id and setVisibility to invisible?
Try starting a new instance of your Activity with a clear stack on the button press (if I'm correct in assuming this comes after C as you described). This way the First Activity instance will still have up to Fragment C and the Second Activity instance will be whatever you like (Fragment A > Fragment D > Fragment F). And you won't need to pop/clear any back stack for any Activity.
HTHs