Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding fragments without saving to history stack?

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.

like image 828
Adam Varhegyi Avatar asked Jan 27 '14 17:01

Adam Varhegyi


People also ask

Can we use fragments without activity?

Android app must have an Activity or FragmentActivity that handles the fragment. Fragment can't be initiated without Activity or FragmentActivity.

How can I maintain fragment state when added to the back stack?

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.

How do I add a fragment to the top of activity?

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.

Are fragments reusable?

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.


2 Answers

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();
like image 98
Orhan Obut Avatar answered Nov 08 '22 21:11

Orhan Obut


By default fragments are not added to backstack. You need to remove call to addToStack() from your code.

like image 35
Marcin Orlowski Avatar answered Nov 08 '22 21:11

Marcin Orlowski