重启活动时调用onStop延迟 [英] onStop delays to be called when restarting activity

查看:155
本文介绍了重启活动时调用onStop延迟的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

重新启动活动时, onStop()从之前的活动延迟太多而无法调用。

When restarting an Activity, onStop() from previous activity delays too much to be called.

I我正在使用此代码重新启动我的活动 PlayerActivity.java

I am using this code to restart my activity PlayerActivity.java

Intent playerIntent = getIntent();
playerIntent.putExtra(Constants.VIDEO_ID, videoId);
playerIntent.putExtra(Constants.CATEGORY_ID, categoryId);
playerIntent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
startActivity(playerIntent);

我们打电话给 PreviousPlayerActivity NewPlayerActivity ,之前的活动和新的活动。 (记住它们是相同的PlayerActivity)。

Let's call PreviousPlayerActivity and NewPlayerActivity, the activity that was before and the new activity. (remembering that they are the same PlayerActivity).

序列

重启应用程序时,活动生命周期中的此流程如下。

When restarting the app follows this flow in the activity-lifecycle.

PreviousPlayerActivity onPause() --> 
NewPlayerActivity onCreate() --> 
NewPlayerActivity onStart() --> 
NewPlayerActivity onResume() --> 
NewPlayerActivity performs a heavy operation --> 
PreviousPlayerActivity onStop() --> 
PreviousPlayerActivity onDestroy()

我需要什么

我需要在NewPlayerActivity启动之前销毁PreviousPlayerActivity。但是, onStop()只是在繁重操作后调用,所以它会在大约10秒钟内被调用。

I need PreviousPlayerActivity to be completed destroyed before NewPlayerActivity starts. However, onStop() is just called after the heavy operation, so it delays around 10 seconds to be called.

我尝试了什么

如果我使用 recreate()方法,它会破坏PreviousPlayerActivity在调用NewPreviousActivity之前,但是通过调用 recreate()我不能将它们放入新的活动实例中。

If I use recreate() method it does destroy PreviousPlayerActivity before calling NewPreviousActivity, but by calling recreate() I can not putExtras into the new activity instance.

问题


  1. 如何在重新启动活动时彻底销毁PreviousActivity?

  2. 是在使用 recreate()

  1. How to completely destroy PreviousActivity when restarting an activity?
  2. Is there a way to putExtras while using recreate()?


推荐答案

在Android开发人员指南的活动生命周期中。

In Activity Lifecycle from Android Developer guide.


协调活动

当一项活动时开始另一个,他们都体验nce生命周期
过渡。第一个活动停止运行并进入Paused
或Stopped状态,同时创建另一个活动。如果这些
活动共享保存到光盘或其他地方的数据,重要的是
了解第一个活动在
创建第二个活动之前没有完全停止。相反,启动第二个
的过程与停止第一个
的过程重叠。

When one activity starts another, they both experience lifecycle transitions. The first activity stops operating and enters the Paused or Stopped state, while the other activity is created. In case these activities share data saved to disc or elsewhere, it's important to understand that the first activity is not completely stopped before the second one is created. Rather, the process of starting the second one overlaps with the process of stopping the first one.

生命周期回调的顺序是明确定义的,特别是当
这两个活动在同一个过程(app)中,一个是另一个开始
。以下是活动A
开始活动B时的操作顺序:

The order of lifecycle callbacks is well defined, particularly when the two activities are in the same process (app) and one is starting the other. Here's the order of operations that occur when Activity A starts Activity B:


  1. 活动A的 onPause() 方法执行。

  2. 活动B的 onCreate() onStart() onResume() 方法按顺序执行。 (活动B现在具有用户关注点。)

  3. 然后,如果活动A在屏幕上不再可见,则其 onStop() 方法执行。

  1. Activity A's onPause() method executes.
  2. Activity B's onCreate(), onStart(), and onResume() methods execute in sequence. (Activity B now has user focus.)
  3. Then, if Activity A is no longer visible on screen, its onStop() method executes.

这个可预测的生命周期回调序列允许你管理
的信息转换从一个活动到另一个活动。

This predictable sequence of lifecycle callbacks allows you to manage the transition of information from one activity to another.

因此,您描述的行为是预期的或可预测的。

So the behavior that you describe is expected or predictable.

回到你的问题。


1.如何彻底销毁PreviousActivity重新开始活动?

1.How to completely destroy PreviousActivity when restarting an activity?




  • 使用重新创建 API,限制是它只适用于API 11或以上

    • Using recreate API, the limitation is it only works from API 11 or above

    • 2.使用recreate()时有什么方法可以putExtras吗?

      2.Is there a way to putExtras while using recreate()?




      • 来自重新创建文档

        • From recreate documentation

        • 重新创建

          public void recreate ()
          

          导致使用新实例重新创建此活动。这导致
          与由于
          配置更改创建Activity时的流程基本相同 - 当前实例将通过其
          生命周期到onDestroy(),然后创建一个新实例之后。

          Cause this Activity to be recreated with a new instance. This results in essentially the same flow as when the Activity is created due to a configuration change -- the current instance will go through its lifecycle to onDestroy() and a new instance then created after it.

          因为活动将被重新创建所以 onSaveInstanceState onRestoreInstanceState 。你可以猜测这个想法是在 onSaveInstanceState 中保存数据,并在 onRestoreInstanceState onCreate <中检索/ code>。

          Because the activity will be recreated so onSaveInstanceState and onRestoreInstanceState will be called as well. As you can guess the idea is save data in onSaveInstanceState and retrieve in onRestoreInstanceState or onCreate.

          步骤1:将数据保存在 onSaveInstanceState

          Step 1: Save data in onSaveInstanceState

          // The key for saving and retrieving isActivityRecreated field.
          private static final String KEY_IS_ACTIVITY_RECREATED = "KEY_IS_ACTIVITY_RECREATED";
          
          /** true if this activity is recreated. */
          private boolean isActivityRecreated = false;
          
          // Call this method when you want to recreate this activity.
          private void recreateActivity() {
              isActivityRecreated = true;
              recreate();
          }
          
          @Override
          protected void onSaveInstanceState(Bundle outState) {
              super.onSaveInstanceState(outState);
              outState.putBoolean(KEY_IS_ACTIVITY_RECREATED, isActivityRecreated);
              outState.putInt(Constants.VIDEO_ID, videoId);
              outState.putInt(Constants.CATEGORY_ID, categoryId);
          }
          

          步骤2:检索 onRestoreInstanceState中的数据 onCreate

          @Override
          protected void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity_main);
          
              if (savedInstanceState != null) {
                  isActivityRecreated = savedInstanceState.getBoolean(KEY_IS_ACTIVITY_RECREATED);
                  if (isActivityRecreated) {
                      // This activity has been recreated.
                      // Reset the flag
                      isActivityRecreated = false;
          
                      // Write your code when this activity recreated.
                      int videoId = savedInstanceState.getInt(Constants.VIDEO_ID);
                      int categoryId = savedInstanceState.getInt(Constants.CATEGORY_ID);
                      ...   
                  }
              }
          }
          

          这篇关于重启活动时调用onStop延迟的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆