Is there any way replace a fragment with FragmentTransaction's replace
, and NOT add it to history stack?
I just want my app to dont remember it, when i push back button.
Android app must have an Activity or FragmentActivity that handles the fragment. Fragment can't be initiated without Activity or FragmentActivity.
Solution: Save required information as an instance variable in calling activity. Then pass that instance variable into your fragment. So following on with my example: before I display F2 I save user data in the instance variable using a callback. Then I start F2, user fills in phone number and presses save.
Add a fragment to an activity You can add your fragment to the activity's view hierarchy either by defining the fragment in your activity's layout file or by defining a fragment container in your activity's layout file and then programmatically adding the fragment from within your activity.
A fragment is a reusable class implementing a portion of an activity. A Fragment typically defines a part of a user interface. Fragments must be embedded in activities; they cannot run independently of activities.
When you use FragmentTransaction to add fragment there is addToBackStack() method, if you don't use it, it won't be in the stack.
// it won't add to stack
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
// fragment will be added to stack
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.addToBackStack("placeholder")
commit();
By default fragments are not added to backstack. You need to remove call to addToStack()
from your code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With