My MainActivity contains 4 fragments. My home-fragment is FragmentA. When I press on back button at FragmentB,C or D, I have to return back to FragmentA. If I am on FragmentA and press back button, I have to moveTasktoBack().
What is the best way to do it?
Check out this answer
Just change it so it returns to your home fragment. In the future try to search before you ask, more often than not the answer already exists.
When You replacing the fragment from home fragment and form A then use like this Like
FragmentTransaction.replace(R.id.content_frame, fragment);
then after that paste this line
FragmentTransaction.addToBackStack(backStateName);
from B and C don't use
FragmentTransaction.addToBackStack(backStateName);
Related
I'm working with the fragments navigation and have a problem removing one from the back stack. Here're specifics:
I have 3 independent fragments (let's name them A1, A2, and A3) and each of them can navigate to fragment B using findNavController().navigate(), which after some actions navigates to fragment C using the same method.
What I need is when the back arrow is pressed I return to the A-fragment(which I started from) avoiding fragment B.
I've tried using app:popUpTo, and popBackStack but it hadn't worked. Also, I'm highly not recommended to use FragmentManager
You can use
findNavController().popUp(2)
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
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
I manage two fragments in my MainActivity.
One of them is a subclass of ListFragment to show a list of items.
The main idea is to navigate to another list view when user tap one of the items, and the user can go back to the previous list view when tapping back button.
The code for transmit to a new list is shown as follow:
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
NewFragment newFragment = new NewFragment();
newFragment.setArguments(getIntent().getExtras());
transaction.replace(R.id.fragment_layout, newFragment);
transaction.addToBackStack(null);
transaction.commit();
However, I just simply exit the application other than going back to the previous view.
What am I doing wrong?
unlike activities, with fragments you have to explicitly add things onto the "back stack". basically, when your app displays a new fragment such that'd you like back to return to the preview fragment, you call FragmentTrasaction.addToBackStack().
the framework handles popping the fragment off the back stack when the user presses back. if you need something more complex, you can override the back button press for your fragment. this question covers that,
Android Fragment handle back button press
The back button closes the top Activity and does not navigate back in your Fragment history. You have to do that yourself with popBackStack()