1

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.

A.s.ALI
  • 1,992
  • 3
  • 22
  • 54

1 Answers1

0
  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); } }

Ramesh R
  • 329
  • 1
  • 6
  • how can I save the data of fragment views and then re invoking the new layout ? – A.s.ALI Aug 23 '16 at 09:55
  • As pointed out in 2 statement. use the someStateValue to set appropriate view. For first name set it as FirstNameTextView.setText(someStateValue) – Ramesh R Aug 23 '16 at 10:00
  • can you please edit point num 2 with the little example . and what if I have to save the other members of the class and also I have a model list view – A.s.ALI Aug 23 '16 at 10:02
  • thanks mate , what do yew mean by that You will receive onConfigurationChanged() callback by retaining activity.it is not related to Fragment.setRetainInstance(true). – A.s.ALI Aug 23 '16 at 10:16
  • I meant to handle the configuration changes our self we generally set android:configChanges="orientation" in manifest for that particular activity. if we do this we will receive onConfigurationChanged() callback in that particular Activity and also in the Fragment displayed in that activity. – Ramesh R Aug 23 '16 at 10:24
  • great help indeed , this is the right answer. can you tell me one thing more , if I rotate the device does my activity re attach the fragment , which I attached programatically in oncreate of MainActivity ? do I need to save instance of fragment also in activity ? – A.s.ALI Aug 23 '16 at 10:27
  • No,You don't need to save the instance of the fragment inside activity. Activity will reattach the fragment during configuration change if it is already added. we can make sure this by checking savedInstanceState ` if (savedInstanceState == null) { // attach fragment } else { // set saved values }` – Ramesh R Aug 23 '16 at 10:38
  • great. by applying your point 2 in my fragment , how can I change the layout , I have designed another layout for landscape version – A.s.ALI Aug 23 '16 at 10:43
  • you mean to say that on configuration changed it will apply it self , All I need to set the data – A.s.ALI Aug 23 '16 at 11:05