亦余心之所善兮,虽九死其犹未悔。这篇文章主要讲述Android 简单案例:onSaveInstanceState 和 onRestoreInstanceState相关的知识,希望能为你提供帮助。
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 简单案例(可移动的View)
- Windows系统添加虚拟串口及CanToolApp功能1的实现
- android selector shape 使用
- Spring Boot Dubbo applications.properties 配置清单
- Android源码解析——LruCache
- Android 动画fillAfter和fillBefore
- 浅谈 MVP in Android
- Android DiskLruCache完全解析,硬盘缓存的最佳方案
- vob文件怎样打开,本文教您打开vob文件的设置