I have 4 button to replace fragment in activity [fragment A , fragment B , fragment C , fragment D] and then I replace fragment A to activity and I change value in fragment A after that I replace fragment B to fragment A and replace fragment C to fragment B . But I want to replace fragment A to fragment C . How to save state in fragment A.
Code when I commit fragment
private void beginFragmentTransaction(BaseFragment fragment) {
String tag = fragment.getClass().getName();
currentFragmentTag = tag;
boolean fragmentPopped = getChildFragmentManager().popBackStackImmediate(tag, 0);
if (!fragmentPopped) {
getChildFragmentManager().beginTransaction()
.replace(R.id.container, fragment, tag)
.addToBackStack(tag)
.commit();
}
}
Diagram to replace
fragment A -------> fragment B
fragment B -------> fragment C
fragment C -------> fragment A
PS. I don't want to use back button to back to fragment A , I want to replace fragment A and restore data in the first commit.
Various Android system operations can affect the state of your fragment. To ensure the user's state is saved, the Android framework automatically saves and restores the fragments and the back stack. Therefore, you need to ensure that any data in your fragment is saved and restored as well.
Like an Activity , a Fragment has its own lifecycle. Understanding the relationship between Activity and Fragment lifecycles helps you design fragments that can save and restore variables and communicate with activities.
A fragment has its layout and its behaviour with its lifecycle callbacks. You can add or remove fragments in the activity. Activity is running, which is in the resumed lifecycle state. You can combine multiple fragments in a single activity to build a multi-pane UI.
A Fragment represents a reusable portion of your app's User Interface. Retained Fragment consists of the configuration change that causes the underlying Activity to be destroyed. The term "retained" refers to the fragment that will not be destroyed on configuration changes.
Instead of restoring the state during onCreate()
you may choose to implement onRestoreInstanceState()
, which the system calls after the onStart()
method. The system calls onRestoreInstanceState()
only if there is a saved state to restore, so you do not need to check whether the Bundle
is null.
FYI : this is a sample code. Just for your reference.
public class MainFragment extends Fragment {
private String title;
private double rating;
private int year;
@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(TITLE, "Gladiator");
savedInstanceState.putDouble(RATING, 8.5);
savedInstanceState.putInt(YEAR, 2000);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
title = savedInstanceState.getString(TITLE);
rating = savedInstanceState.getDouble(RATING);
year = savedInstanceState.getInt(YEAR);
}
}
FYI : This really a good thread check this also Once for all, how to correctly save instance state of Fragments in back stack?
If you want to save the state of previous tabs and don't want to refresh/recreate view use this code and change the value according to the tabs limit
ViewPager mViewPager = (ViewPager)findViewById(R.id.pager);
mViewPager.setOffscreenPageLimit(2);
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