重新启动时调用 onStop 延迟 activity
onStop delays to be called when restarting activity
重新启动 Activity 时,之前 activity 的 onStop()
延迟太多,无法调用。
我正在使用此代码重新启动我的 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,之前的activity和新的 activity。 (记住他们是同一个 PlayerActivity)。
序列
重新启动应用程序时遵循 activity-生命周期中的此流程。
PreviousPlayerActivity onPause() -->
NewPlayerActivity onCreate() -->
NewPlayerActivity onStart() -->
NewPlayerActivity onResume() -->
NewPlayerActivity performs a heavy operation -->
PreviousPlayerActivity onStop() -->
PreviousPlayerActivity onDestroy()
我需要的
我需要在 NewPlayerActivity 开始之前完成销毁 PreviousPlayerActivity。但是,onStop()
是在繁重的操作之后才被调用,所以它会延迟大约 10 秒后被调用。
我试过的
如果我使用 recreate()
方法,它会在调用 NewPreviousActivity 之前销毁 PreviousPlayerActivity,但是通过调用 recreate()
我无法将 Extras 放入新的 activity实例.
问题
- 如何在重新启动activity时完全销毁上一个Activity?
- 有没有办法在使用
recreate()
的同时放置 Extras?
您只需在 AndroidManifest.xml
文件中将 noHistory 标志设置为 true 即可。我认为根据您的要求最小化时不需要保持 activity 状态。
<activity
android:name=".PlayerActivity"
android:noHistory="true" />
来自 Android 开发人员指南的 Activity Lifecycle。
Coordinating activities
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.
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:
- Activity A's
onPause()
method executes.
- Activity B's
onCreate()
, onStart()
, and onResume()
methods execute in sequence. (Activity B now has user focus.)
- 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.
因此,您描述的行为是预期的或可预测的。
回到你的问题。
1.How to completely destroy PreviousActivity when restarting an activity?
- 使用 recreate API,限制是它只适用于 API 11 或以上
2.Is there a way to putExtras while using recreate()?
- 来自 recreate 文档
recreate
public void recreate ()
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.
因为 activity 将被重新创建,所以 onSaveInstanceState and onRestoreInstanceState 也会被调用。正如您所猜测的那样,这个想法是将数据保存在 onSaveInstanceState
中并在 onRestoreInstanceState
或 onCreate
中检索。
第 1 步:将数据保存在 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);
...
}
}
}
重新启动 Activity 时,之前 activity 的 onStop()
延迟太多,无法调用。
我正在使用此代码重新启动我的 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,之前的activity和新的 activity。 (记住他们是同一个 PlayerActivity)。
序列
重新启动应用程序时遵循 activity-生命周期中的此流程。
PreviousPlayerActivity onPause() -->
NewPlayerActivity onCreate() -->
NewPlayerActivity onStart() -->
NewPlayerActivity onResume() -->
NewPlayerActivity performs a heavy operation -->
PreviousPlayerActivity onStop() -->
PreviousPlayerActivity onDestroy()
我需要的
我需要在 NewPlayerActivity 开始之前完成销毁 PreviousPlayerActivity。但是,onStop()
是在繁重的操作之后才被调用,所以它会延迟大约 10 秒后被调用。
我试过的
如果我使用 recreate()
方法,它会在调用 NewPreviousActivity 之前销毁 PreviousPlayerActivity,但是通过调用 recreate()
我无法将 Extras 放入新的 activity实例.
问题
- 如何在重新启动activity时完全销毁上一个Activity?
- 有没有办法在使用
recreate()
的同时放置 Extras?
您只需在 AndroidManifest.xml
文件中将 noHistory 标志设置为 true 即可。我认为根据您的要求最小化时不需要保持 activity 状态。
<activity
android:name=".PlayerActivity"
android:noHistory="true" />
来自 Android 开发人员指南的 Activity Lifecycle。
Coordinating activities
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.
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:
- Activity A's
onPause()
method executes.- Activity B's
onCreate()
,onStart()
, andonResume()
methods execute in sequence. (Activity B now has user focus.)- 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.
因此,您描述的行为是预期的或可预测的。
回到你的问题。
1.How to completely destroy PreviousActivity when restarting an activity?
- 使用 recreate API,限制是它只适用于 API 11 或以上
2.Is there a way to putExtras while using recreate()?
- 来自 recreate 文档
recreate
public void recreate ()
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.
因为 activity 将被重新创建,所以 onSaveInstanceState and onRestoreInstanceState 也会被调用。正如您所猜测的那样,这个想法是将数据保存在 onSaveInstanceState
中并在 onRestoreInstanceState
或 onCreate
中检索。
第 1 步:将数据保存在 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);
...
}
}
}