import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; 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)); } }
Android 简单案例:onSaveInstanceState 和 onRestoreInstanceState
转载本文章为转载内容,我们尊重原作者对文章享有的著作权。如有内容错误或侵权问题,欢迎原作者联系我们进行内容更正或删除文章。
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Android笔记:onSaveInstanceState和onRestoreInstanceState总结
Android笔记:onSaveInstanceState和onRestoreInstanceState总结
保存 Android onSaveInstanceState -
onSaveInstanceState & onRestoreInstanceState
前面两节主要详细讲解了Activity的生命周期方法以及一些特定的场景,其中涉及到onSaveInstanc
home键 数据 屏幕切换 bundle 生命周期 -
安卓onSaveInstanceState和onRestoreInstanceState触发时机
简要介绍很多介绍安卓生命周期的文
android bundle ide -
onSaveInstanceState(),onRestoreInstanceState的掉用时机
onSaveInstanceState(),onRestoreInstanceState的掉用时机
android 屏幕方向 activity启动 重启 -
Android onSave Android onsaveinstancestate
在Android系统中,有时系统可能因为系统资源不够而杀死(kill)某些Activity,在kill Activity之前会调用 onSaveInstanceState来保存一些状态信息(当然也可以保存其他信息),当再次回到该Activity时,系统会调用onRestoreInstanceState来恢复数据。 下面先讲一下onSaveInsta
Android onSave Android onSaveInstance onRestoreInstance PersistableBundle