3

I have seen the following links before posting this question

http://www.devx.com/wireless/Article/40792/1954

Saving Android Activity state using Save Instance State

http://www.gitshah.com/2011/03/how-to-handle-screen-orientation_28.html

How to save state during orientation change in Android if the state is made of my classes?

I am not getting how should i override the following function :

@Override
    public Object onRetainNonConfigurationInstance() {
        return someExpensiveObject;
    }

In my application i have layout with one editext visible and other editext get visible when the data of first editext validates to true.I have set the visbility of all other editextes and textviews to false and make them visible after validating.

So in my activity if the screen orientation is changed then all the items having android:visibility="false" get invisible.

I have also came to know that when our activities screen orientation changes it calls onStop() followed by onDestroy() and then again starts a fresh activity by calling onCreate()

This is the cause .. But i am not getting how to resolve it ..

Here You can see the screenshots of my application :

enter image description here in this image all fields are loaded and in another image when the screen orientation is changed to landscape they are all gone

enter image description here

Any link to tutorial or piece of code will be highly appreciable.

And also my application crashes when a progress dialog is shown up and i try to change screen orientation.How to handle this ??

Thanks

Community
  • 1
  • 1
Shruti
  • 1
  • 13
  • 55
  • 95
  • a good tutorial on how to handle an activity state using saved instance state bundle check http://www.quicktips.in/handling-activity-state-using-saved-instance-state-bundle/ – Deepak Swami Aug 25 '15 at 02:44

3 Answers3

7

Well if you have the same layout for both screens then there is no need to do so just add below line in your manifest in Activity node

android:configChanges="keyboardHidden|orientation"

for Android 3.2 (API level 13) and newer:

android:configChanges="keyboardHidden|orientation|screenSize"

because the "screen size" also changes when the device switches between portrait and landscape orientation.

From documentation here: http://developer.android.com/guide/topics/manifest/activity-element.html

General Grievance
  • 4,555
  • 31
  • 31
  • 45
ingsaurabh
  • 15,249
  • 7
  • 52
  • 81
  • Thanks for replying and it worked for me ... So then y this `onRetainNonConfigurationInstance()` function is used ?? – Shruti Nov 11 '11 at 11:39
  • @Shruti its used if you have different layouts files the you have to save the data on layout change because layouts swap in this condition – ingsaurabh Nov 11 '11 at 12:07
  • Ok and how should i prevent the application from crashing when somebody changes orientation while progress bar is showing up ? – Shruti Nov 11 '11 at 12:12
  • Once you use the above method the activity is not restarted hence the progressbar will also not close and then your app will not be crashed – ingsaurabh Nov 11 '11 at 12:14
  • 1
    You would have to use `onSaveInstanceState(Bundle)` to save each of those views' visibilities, I'd imagine. – Oleg Vaskevich Dec 24 '12 at 21:16
1

There is another possibility using which you can keep the state as it is even on Orientation change using the onConfigurationChanged(Configuration newConfig).

Called by the system when the device configuration changes while your activity is running. Note that this will only be called if you have selected configurations you would like to handle with the configChanges attribute in your manifest. If any configuration change occurs that is not selected to be reported by that attribute, then instead of reporting it the system will stop and restart the activity (to have it launched with the new configuration).

At the time that this function has been called, your Resources object will have been updated to return resource values matching the new configuration.

vinod
  • 8,350
  • 9
  • 32
  • 36
0

There are 2 ways of doing this, the first one is in the AndroidManifest.xml file. You can add this to your activity's tag. This documentation will give you an in depth explanation, but put simply it uses these values and tells the activity not to restart when one of these values changes.

android:configChanges="keyboardHidden|orientation|screenSize|screenLayout"

And the second one is: overriding onSaveInstanceState and onRestoreInstanceState. This method requires some more effort, but arguably is better. onSaveInstanceState saves the values set (manually by the developer) from the activity before it's killed, and onRestoreInstanceState restores that information after onStart() Refer to the official documentation for a more in depth look. You don't have to implement onRestoreInstanceState, but that would involve sticking that code in onCreate().

In my sample code below, I am saving 2 int values, the current position of the spinner as well as a radio button.

 @Override
    public void onSaveInstanceState(@NonNull Bundle savedInstanceState) {
        spinPosition = options.getSelectedItemPosition();
        savedInstanceState.putInt(Constants.KEY, spinPosition);
        savedInstanceState.putInt(Constants.KEY_RADIO, radioPosition);
        super.onSaveInstanceState(savedInstanceState);

    }

    // And we restore those values with `getInt`, then we can pass those stored values into the spinner and radio button group, for example, to select the same values that we saved earlier. 

    @Override
    public void onRestoreInstanceState(@NotNull Bundle savedInstanceState) {
        spinPosition = savedInstanceState.getInt(Constants.KEY);
        radioPosition = savedInstanceState.getInt(Constants.KEY_RADIO);
        options.setSelection(spinPosition, true);
        type.check(radioPosition);
        super.onRestoreInstanceState(savedInstanceState);
    }
A P
  • 2,131
  • 2
  • 24
  • 36