Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save state in fragment

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.

like image 870
Wuttipong Khemphetjetsada Avatar asked Jun 11 '17 04:06

Wuttipong Khemphetjetsada


People also ask

How do I save a fragment state?

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.

Do fragments have their own lifecycle?

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.

What are fragment States?

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.

What is retained fragment?

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.


2 Answers

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?

like image 65
Sudheesh R Avatar answered Sep 22 '22 04:09

Sudheesh R


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); 
like image 20
Waheed Sabir Avatar answered Sep 21 '22 04:09

Waheed Sabir