I created fragment-A part of Activity-A, here I HIDE fragment-A and launch fragment-B, working fine. Now I detach fragment-B, so how Fragment-A come to know that now it's time to wakeup i.e. SHOW.
use addToBackStack method which is FragmentTransaction class. The transaction will be remembered and when you press the back button while Fragment B is active, Fragment A will be shown.
Override Fragment B's onDetach() method to unhide fragment A.
In case you need, this is a good guide on how to have fragments/activity interact with each other: http://developer.android.com/training/basics/fragments/communicating.html
Related
I have the following scenario:
FragmentA (pressing next)--> Activity(automatically starts)--> FragmentB.
Because of the Android library that I'm currently using, I must start my fragmentB automatically through an activity.
I tried android:noHistory="true" on my activity but I still have the following unwanted behavior:
Current behavior: When pressing back button on FragmentB, I'm going to my activity, then when I press it again I go to FragmentA.
My activity only has a toolbar and nothing else.
I would like to go back to FragmentA when pressing the back button on FragmentB
getActivity().finish() ;
getActivity().overridePendingTransition(0,0);
will kill the activity along with the fragment and make the transition instant.
Try adding the fragment to the backstack while performing the FragmentTransaction.
getSupportFragmentManager().beginTransaction()
.add(detailFragment, "detail")
.addToBackStack() // Add this transaction to the backstack
.commit();
when using fragments in your app, individual FragmentTransaction objects may represent context changes which can cause loosing the previous fragments and required to be added to the back stack while performing FragmentTransaction. To understand the back navigation in android in better way and detailed explanation you can refer this
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
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.
I have two fragments,
fragmentA in foreground
now I show fragmentB with FragmentTransaction.add(id, Fagment), (not .replace) so the fragmentA is still alive, with fragmentB on top of it,
now I use back button, here the fragmentB is destroyed, leaving fragmentA visible,
at this moment, how would I know that fragmentA has returned to the "foreground", ie onResume,
note that onResume is not called, due to FragmentTransaction.add(id, Fagment), in other words, fragmentA doesn't go onPause when fragmentB is shown
thank you very much for your help
If you want to know when fragment A becomes visible again, you can first hide it in the fragment transaction that creates fragment B:
fragmentTransaction.add(id, fragmentB).hide(fragmentA).addToBackStack(null);
Then in Fragment A, override onHiddenChanged:
#Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
// Handle visibility changed. Note this method is called only when the state is changed.
}
When the back button is pressed, the fragment transaction will be reversed and the fragment's state will be changed to visible. One thing you have to watch out for: I've noticed that the hidden state isn't preserved between activity rotation so you'd have to perform your own bookkeeping in onSaveInstanceState. I do something similar to what you're asking since in my case the fragment views are expensive to recreate.
Before doing this though, you might want to consider handling your fragments another way, like with .replace() instead of .add(). If your fragment is completely hidden by the new fragment, then maybe you don't need to keep it around, and you can let the fragment manager bring it back once the user hits the back button. That way, you can just use the normal lifecycle functions like onPause and onResume.
I got the app, that uses practcally just one activity. There is main area where I put fragments into. But what about back button now?
Of course I can override onBackPressed() but with what?
I'm pushing a fragment into it's holder using FragmentTransaction.replace() method every time. I might be lacking understanding of a subject, but shouldn't there be some fragment built-in stack, that would allow me to point onBackPressed() to the previous fragment in stack?
Do not override onBackPress. You can add any fragment transaction to back stack. There is special method to do this.