How to save the fields data while changing the Lay

2019-08-01 02:17发布

问题:

I have read a lot of things about the data save instance and restore but Unable to implement in my case. What is my case in application .

  1. I am using the Activity (MainActivity) and calling the fragment in it let say ParentFragment
  2. ParentFragment Calls the ChildFragment in it though ParentFragment has its own views such as TextViews which takes First name, Last name and age and below this part I am programatically calling the ChildFragment

So in this way I have 2 fragments in the MainActivity, which is being shown to the user at a time on the screen

**What I want **

  1. I want when User has change the orientation the layout should also change but I also want that the text fields should maintain there data in them .
  2. I want to save some more fragment variables and their data also on configuration changes and retrieve them after the new layout is set after screen orientation changed.

**Problems and Confusions **

  1. I have no idea If i set the Fragment.setRetainInstance(true) then would my fragment still be able to receive the onConfiguration Change call back?

  2. When I rotate my device, the fragment gets re-initialize and also my activity has the Asynctask and that runs again , i want my activity to hold the same data . How can I achieve that?

Please help me and give me some hint.

回答1:

  1. If you want to change layout on orientation change then,you should not handle configuration change by retaining the activity(by setting android:configChanges="orientation" value for corresponding activity defined in manifest) or by using setRetainInstance() in fragment.
  2. For saving the fragment state on configuration change use
    @Override protected void onSaveInstanceState(Bundle outState) { outState.putInt(FIRST_NAME_VALUE_KEY, firstNameTextView.getText()); outState.putInt(LAST_NAME_VALUE_KEY, lastNameTextView.getText()); super.onSaveInstanceState(outState); }
    and for writing back state to the views in fragment use
    @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.my_fragment, container, false); if (savedInstanceState != null) { String firstName = savedInstanceState.getInt(FIRST_NAME_VALUE_KEY); String lastName = savedInstanceState.getInt(LAST_NAME_VALUE_KEY); firstNameTextView.setText(firstName); lastNameTextView.setText(lastName); } return view; }

  3. You will receive onConfigurationChanged() callback by retaining activity.it is not related to Fragment.setRetainInstance(true).

  4. To avoid running asynctask again,We can store data and retain it using following callbacks inside the activity.
    @Override protected void onSaveInstanceState(Bundle savedInstanceState) { // Save custom values into the bundle savedInstanceState.putInt(SOME_VALUE, someIntValue); savedInstanceState.putString(SOME_OTHER_VALUE, someStringValue); // Always call the superclass so it can save the view hierarchy state super.onSaveInstanceState(savedInstanceState); }

      in onCreate(Bundle savedInstanceState) call back you can check for savedInstanceSate , based on that you can call u asynctask or retain values as below
    @Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
 if (savedInstanceState == null) { asyncTask.execute(); } else { someIntValue = savedInstanceState.getInt(SOME_VALUE); someStringValue = savedInstanceState.getString(SOME_OTHER_VALUE); } }