Reputation: 1442
I found several questions regarding this and it appears that I have to correctly use onSavedInstanceState and onRestoreInstanceState methods.
My application creates an array of cards and shows them in a gridview, each grid containing a textview.
While in the app, after adding cards; if I go out of the application using the menu button, everything resumes fine after comming back. However, on orientation change all the 'table' is reset; all the cards have to be added again.
So, why do I loose information on screen orientation change and not on exiting and reentering the app. How can I fix that?
The mentioned methods only have this:
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
}
My onCreate method begins with:
super.onCreate(savedInstanceState);
Upvotes: 0
Views: 167
Reputation: 897
A change in orientation of the device recreates the entire activity - calling onCreate() all over again. Whereas when you use the home button the activity is paused (onPause()) and then when it is made visible again it enters through the onResume() method. Hence anything done in onCreate() is retained.
http://developer.android.com/reference/android/app/Activity.html
I think this is probably what you are looking for
http://developer.android.com/guide/topics/resources/runtime-changes.html
Upvotes: 1
Reputation: 2446
here below i put a code of multiresolution sample application
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));
}
}
Upvotes: 0