FragmentA (pressing next)--> Activity(automatically starts)--> FragmentB - android

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

Related

How to send data back from Activity to Activity with Navigation Component

I know Navigation Component is ideally designed to be used in one-single-activity app. However, Android allows now add <activity> items within a NavGraph. My app is kind of following the one-single-act pattern, but there is a moment where FragmentA has to navigate a DialogFragment.
I would navigate to it by using:
Navigation.findNavController(v).navigate(R.id.actionToFragmentB);
but I cannot because the problem described here, so I ended up deciding to navigate to this Fragment by navigating to an ActivityB and placing the Fragment within it by using FragmentTransaction
Navigation.findNavController(v).navigate(R.id.actionToAcvitityB);
Summing up, my approach has to navigate from FragmentA within ActivityA to ActivityB, and this last will be in charge of showing the FragmentB by FragmentTransaction. Or, I have to navigate from FragmentA within ActivityA to, ¿¿somehow??, FragmentB.
My real problem is: how do I send data back from ActivityB to ActivityA??? Or from FragmentB to the FragmentA??? Taking into account that having two Activities I do not have the same NavController.

Android - How to avoid onResume of fragments when popping them from the backstack

We have multiple screens in our app and to switch between them we use FragmentTransactions, primarily replace, and we add fragments to the backstack when we do so. While doing so we stay within the same MainActivity.
So if we transition from Fragment A to Fragment B via some button we now have a stack that looks like
B
A
If we go to another Fragment C it goes on top of the stack like so
C
B
A
However, sometimes we transition to a fragment D, such that we want to wipe out the backstack so that users can no longer navigate back through C, B, A. We now want our backstack after navigating to D to look like this.
D
But in order to do so we need to clear the backstack using:
FragmentManager supportFragmentManager = getActivity().getSupportFragmentManager();
while(supportFragmentManager.getBackStackEntryCount() > 0)
{
supportFragmentManager.popBackStackImmediate();
}
clearingBackStack = false;
But when we do so, the onResume of fragments C,B and A are called. This is not the functionality we intend and can have negative consequences such as making unnecessary server calls. We have also noticed on low end devices that we see a flash of the popped fragments as they come off the stack.
We wish to avoid this behavior, is there a way to pop the entire fragment backstack without the popped fragments being activated in anyway?

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

Back to Home Fragment when I press on back button

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);

back button doesn't work when I want to return to the previous fragment

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()

Categories

Resources