android - 为什么 onStart 之后不调用 onRestoreInstanceState?

标签 android

我正在尝试测试 onRestoreInstanceState 方法以及何时(确切地)调用它。所以我遵循了这些步骤:

  1. 开始我的 Activity 。 onCreate --> onStart --> onResume 被调用。
  2. 按模拟器上的主页按钮。 onPause --> onSaveInstanceState --> onStop 被调用。
  3. 单击启动器中的图标并再次启动我的 Activity 。 onRestart --> onStart --> onResume 被调用。

我的java代码:

package com.test.demostate.app;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;


 public class MainActivity extends ActionBarActivity {
   private int visiters=0;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       Log.d("TAG","onCreate");

   }

  @Override
   protected void onPause() {
      super.onPause();
       Log.d("TAG","onPause");
  }

  @Override
   protected void onStop() {
       super.onStop();
       Log.d("TAG","onStop");
   }

   @Override
   protected void onStart() {
       super.onStart();
       Log.d("TAG","onStart");
   }

   @Override
    protected void onRestart() {
       super.onRestart();
       Log.d("TAG","onRestart");

   }

    @Override
     protected void onResume() {
       super.onResume();
        visiters++;
        Log.d("TAG","onResume");
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("visiters",visiters);
        Log.d("TAG",visiters+" visiters  was saved ");
    }

    @Override
     protected void onRestoreInstanceState(Bundle savedInstanceState) {
         super.onRestoreInstanceState(savedInstanceState);
        visiters=savedInstanceState.getInt("visiters");
         Log.d("TAG",visiters+" visiters was restored");
    }

     @Override
    protected void onDestroy() {
        super.onDestroy();
        Log.d("TAG","onDestroy");
    }
}

来自文档:您可以选择实现 onRestoreInstanceState(),而不是在 onCreate() 期间恢复状态,系统会在 onStart() 方法之后调用它。

所以调用onRestoreInstanceState

  • Activity 被销毁后 onPause --> onStop --> onDestroy 然后是 onCreate --> onRestoreInstanceState --> onResume(例如由于屏幕旋转)
  • Activity 停止后 onPause --> onStop --> onRestart --> onStart --> onRestoreInstanceState --> onResume(例如由于按下主页图标)

但是为什么不在 onStart 之后调用呢?

谢谢

最佳答案

仅当 onSaveInstanceState() 已被调用时,才在 onStart() 之后。

来自文档:

This method is called after onStart() when the activity is being re-initialized from a previously saved state, given here in savedInstanceState. Most implementations will simply use onCreate(Bundle) to restore their state, but it is sometimes convenient to do it here after all of the initialization has been done or to allow subclasses to decide whether to use your default implementation. The default implementation of this method performs a restore of any view state that had previously been frozen by onSaveInstanceState(Bundle).

This method is called between onStart() and onPostCreate(Bundle).

Activity#onRestoreInstanceState()

关于android - 为什么 onStart 之后不调用 onRestoreInstanceState?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24130726/

相关文章:

java - 如何将移动应用程序连接到数据库

java - 如何在 YouTube API 中获取与一个 Google 帐户关联的多个用户名?

android - 我想在 ExpandableListView 中为组元素设置可见性

android - 如何在 Google Play 商店上传超过 50MB 的应用程序(Android apk 文件)?

安卓工作室 : Gradle Build Error No repositories are defined

android - Android 中的水平滚动页脚菜单

安卓开发 : Service get killed

android - 3D 引擎对我有什么用?

java - 如何将字符串中存储的数据转换为字符串数组

android - cordova相机copyFile错误代码1000