Restart fragment after back button - android

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.

Related

Cannot update fragment's data (fragment in viewpager)

I have 3 activites- A,B, and C. All these 3 activities have a viewpager. And these viewpagers have 1 fragment(this fragment is common to all viewpagers). The launcher activity is A, and pressing a button in A takes me to B, and pressing a button in B takes me to C.
A-> B -> C
Now the problem is when I update any data in C's fragment, it is not updated in A's and B's fragment. But if I update any data in A's fragment and then go to B, B's fragment will have updated data but vice versa is not happening.
Note: I' using a viewpager because I'll be adding more fragments in the future.
I assume the reason this is happening is because when C's fragment is updated, A's and B's fragment is having the old instance of the viewpager (or fragment). So, when I come back to A or B, the old data is being shown.
If this is the problem what is the best solution?
I have tried using viewpager.notifyDataSetChanged(),this works but messes up the views in the fragment.
Can anybody suggest an alternative?Thank you in advance!

getTargetFragment().onActivityResult() doesn't dismiss the fragment

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.

FragmentTabHost run Fragment onCreateView after press back

I have activity A with FragmentTabHost and activity B. Suppose I go from activity A to activity B via Intent. And then, when I press back button I go to activity A, but Fragment's onCreateView doesn't called. Why? How I can run this method manually?

Fragment onResume() and backStack issue

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.

Hide all Fragments in Backstack but still being able to go back

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

Categories

Resources