java - getLifecycle().getState() 在 onStart() 之后仍然是 CREATED

标签 java android kotlin android-lifecycle

我正在 Activity 上调用一些回调,该功能取决于应用程序和 Activity 的状态,并在应用程序处于前台和后台时被调用。我还记录了我的 Activity 生命周期方法调用。根据 documentation here ,一旦调用 onStart()getCurrentState() 应该是 STARTED,但我发现它始终是 CREATED,我不知道为什么。

@Override
protected void onCreate(@Nullable Bundle bundle) {
    super.onCreate(bundle);
    Log.d(TAG, String.format("%s onCreate()", getClass().getName()));

    //... more code
}

@Override
protected void onStart() {
    super.onStart();
    Log.d(TAG, String.format("%s onStart()", getClass().getName()));

    //... more code
}

@Override
protected void onSaveInstanceState(Bundle bundle) {
    super.onSaveInstanceState(bundle);
    Log.d(TAG, String.format("%s onSaveInstanceState()", getClass().getName()));

    //... more code
}

@Override
protected void onResume() {
    super.onResume();
    Log.d(TAG, String.format("%s onResume()", getClass().getName()));

    //... more code
}

@Override
public onAppBackgrounded() {
    String class = getClass().getName();
    Lifecycle.State state = getLifecycle().getCurrentState();
    Log.d(TAG, String.format("%s onAppBackgrounded() - %s", class, state);

    //... more code
}

@Override
public onAppForegrounded() {
    String class = getClass().getName();
    Lifecycle.State state = getLifecycle().getCurrentState();
    Log.d(TAG, String.format("%s onAppForegrounded() - %s", class, state);

    //... more code
}

onAppBackgrounded()onAppForegrounded() 只是向观察应用程序生命周期的 LifecycleObserver 注册的回调方法。

@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onLifecycleEventStart() {
    callbackRegistrar.onApplicationForegrounded()
}

@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onLifecycleEventStop() {
    callbackRegistrar.onApplicationBackgrounded()
}

这是我在前台应用程序时看到的内容:

2019-07-22 17:51:

19.070 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onCreate()
2019-07-22 17:51:19.989 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onStart()
2019-07-22 17:51:19.990 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onApplicationForegrounded() - CREATED
2019-07-22 17:51:20.013 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onResume()

以下是我在后台应用程序时看到的内容:

2019-07-22 1

8:05:02.527 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onPause()
2019-07-22 18:05:02.664 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onStop()
2019-07-22 18:05:02.703 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onSavedInstanceState()
2019-07-22 18:05:03.229 4754-4754/com.myapp D/TAG: com.myapp.MyActivity onApplicationBackgrounded() - CREATED

而且,当我向状态更改监听器注册了超过 1 个 Activity 时,它们都处于 CREATED 状态。不太确定我错过了什么。文档说,如果在 onStop() 之前调用 onSavedInstanceState() ,就会执行此操作,但这里显然不会发生这种情况。

理想情况下,我想使用 getLifecycle().getState() 来判断当应用程序处于后台和前台时哪个 Activity 位于前台。

最佳答案

它的发生是由于 Activity 生命周期。在 Activity 生命周期中,对于以下情况,onStop() 应该转到 onCreate()

Once your activity is stopped, the system might destroy the process that contains the activity if the system needs to recover memory. Even if the system destroys the process while the activity is stopped, the system still retains the state of the View objects (such as text in an EditText widget) in a Bundle (a blob of key-value pairs) and restores them if the user navigates back to the activity.

更多详情请参阅Activity Life Cycle

请参阅同一页面上的生命周期图

关于java - getLifecycle().getState() 在 onStart() 之后仍然是 CREATED,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57154829/

相关文章:

android - Kotlin 类转换异常

java - 如何在 Java VisualVM 中查看内存分配堆栈跟踪

android - 使用 Android 加速度传感器进行距离测量

android - 为什么 ScrollView 和应用程序移动缓慢(滞后)

android - 如何使用 Jetpack Compose 在 TopAppBar 中设置渐变背景

android - 从 Kotlin RecyclerView(cardview) onclick 加载详细信息 View

java - Jenkins 部署插件。内存不足

java - 当我使用Java Access Bridge捕获元素时,但我无法获取元素的父元素;

java - 比较java中的两个字符串并识别重复的单词

android - 无法在代码中使用 Textviews 和按钮