I have two activities and I use android:configChanges="keyboardHidden|orientation|screenSize"
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
setContentView(R.layout.activity_main);
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
}
}
One active use for portrait to landscape orientation of the second but when the orientation changes, activity is loaded and data is lost
How can I save the data and change the activity orientation?
You can save any Object by Overriding public Object onRetainNonConfigurationInstance () and calling getLastNonConfigurationInstance() in your onCreate method. but if you do this, you have to change your manifest and code, so the normal process for a configuartion change is used.
When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.
If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes. You can declare multiple configuration values in the attribute by separating them with a pipe | character.
If you have small data, you can save and restore it using onSavedInstanceState
and onRestoreInstanceState
.. for details go through this link Saving data
But in case, you have large data then I must say, you should not allow for the orientation changes(which force your activity to recreate). You can restrict it by adding below line in manifest file :
android:configChanges="orientation|keyboardHidden" // fixes orientation
See onSaveInstanceState(Bundle)
and onRestoreInstanceState(Bundle)
I recommend this post
http://www.androiddesignpatterns.com/2013/04/retaining-objects-across-config-changes.html
to anyone who is still looking for a solution to this problem. The author describes how to use a Fragment to retain data.
Make sure to have the call
setRetainInstance(true);
in the onCreate()
method of your Fragment!
you should check sample application "Multiresolution" here below you can see the snippet of code of "Multiresolution"
public final class MultiRes extends Activity {
private int mCurrentPhotoIndex = 0;
private int[] mPhotoIds = new int[] { R.drawable.sample_0,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3,
R.drawable.sample_4, R.drawable.sample_5, R.drawable.sample_6,
R.drawable.sample_7 };
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
showPhoto(mCurrentPhotoIndex);
// Handle clicks on the 'Next' button.
Button nextButton = (Button) findViewById(R.id.next_button);
nextButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
mCurrentPhotoIndex = (mCurrentPhotoIndex + 1)
% mPhotoIds.length;
showPhoto(mCurrentPhotoIndex);
}
});
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("photo_index", mCurrentPhotoIndex);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
mCurrentPhotoIndex = savedInstanceState.getInt("photo_index");
showPhoto(mCurrentPhotoIndex);
super.onRestoreInstanceState(savedInstanceState);
}
private void showPhoto(int photoIndex) {
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageResource(mPhotoIds[photoIndex]);
TextView statusText = (TextView) findViewById(R.id.status_text);
statusText.setText(String.format("%d/%d", photoIndex + 1,
mPhotoIds.length));
}
}
You can save any Object by Overriding public Object onRetainNonConfigurationInstance ()
and calling getLastNonConfigurationInstance()
in your onCreate method.
@Override
public Object onRetainNonConfigurationInstance() {
return data;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
data = getLastNonConfigurationInstance();
}
but if you do this, you have to change your manifest and code, so the normal process for a configuartion change is used.
Different from the SavedInstance method, this only saves the object if the activity is killed because of a configuaration change
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