gpt4 book ai didi

java - 应用程序关闭后恢复 Android 返回堆栈

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:38:59 26 4
gpt4 key购买 nike

管理/恢复应用程序的最佳实践是什么 back stack在多个 session 之间?

工作流程示例:

  1. Activity A 开始(堆栈:A)
  2. Activity B 开始(堆栈:A B)
  3. Activity C 开始(堆栈:A B C)
  4. ...
  5. 用户使用不同的应用程序(假设是 GMail 应用程序)一段时间
  6. ...
  7. 用户返回到我的应用程序,但返回堆栈已被 Android 清除。

在第 7 步,我想让 Activity C 恢复,如果用户按下后退按钮 2 次,它将返回到 Activity B,然后是 Activity A。

[编辑] 添加细节。

在上面的第 7 步之后,Android 中默认发生的是这样的:

  1. Activity A 启动(堆栈:空,并添加 C)

而且我希望用户感觉他仍在使用相同的 session :

  1. Activity C 恢复(堆栈:A B C)
  2. 用户按下后退按钮,Activity B 恢复(堆栈:A B)
  3. 用户按下后退按钮,Activity A 恢复(堆栈:A)

在避免内存泄漏的同时解决这种情况的好方法是什么?

[第二次编辑]我一直在设计一个解决方法,使用通用类 UIController 处理所有 Activity ,并使用 LauncherActivity 将逻辑委托(delegate)给 UIController。

因为我只需要在 ActivityC 启动时重建返回堆栈,这个解决方案似乎工作正常:

public class UIController
{
private boolean _launched = false;

static private final UIController __instance = new UIController();
static public UIController getInstance() { return __instance; }

// Enforces the Singleton Pattern by preventing external access to constructor
private UIController() { }

public void onActivityCreated(Activity activity) {
if (!_launched)
{
if ( shouldRebuildStack() )
{
// Rebuild Activity stack

// Npte : actually Android will add ActivityA and ActivityB to the stack
// but will *NOT* create them right away. Only ActivityC will be
// created and resumed.
// Since they are in the back stack, the other activities will be
// created by Android once needed.
startActivity(activity, ActivityA.class);
startActivity(activity, ActivityB.class);
startActivity(activity, ActivityC.class);
} else {
// Starts default activity
startActivity(activity, ActivityA.class);
}

_launched = true;
}
}

public void onActivityResumed(Activity activity) {
memorizeCurrentActivity( activity.getClass().toString() );
}

private void memorizeCurrentActivity( String className ) {
// write className to preferences, disk, etc.
}

private boolean shouldRebuildStack() {
String previousActivity = " [load info from file, preferences, etc.] ";
return (previousActivity != null && previousActivity.equals("my.package.ActivityC"));
}

private void startActivity(Activity caller, Class newActivityClass)
{
Intent intent = new Intent(caller, newActivityClass);
intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
caller.startActivity( intent );
}
}

// This is the default activity in the AndroidManifest.xml
// This prevents ActivityA from starting right away if the UIController
// wants to rebuild the stack.
public class LauncherActivity() {
protected void onCreate(Bundle data) {
super.onCreate(data);
UIController.getInstance().onActivityCreated(this);
finish();
}
}

public class ActivityA() {
protected void onCreate(Bundle data) {
super.onCreate(data);
UIController.getInstance().onActivityCreated(this);
}
protected void onResume() {
super.onResume();
UIController.getInstance().onActivityResumed(this);
}
}

public class ActivityB() {
// onCreate() & onResume(), same as ActivityA
}

public class ActivityC() {
// onCreate() & onResume(), same as ActivityA
}

public class LauncherActivity() {
protected void onCreate(Bundle data) {
super.onCreate(data);
UIController.getInstance().onActivityCreated(this);
finish();
}
}

public class ActivityA() {
protected void onCreate(Bundle data) {
super.onCreate(data);
UIController.getInstance().onActivityCreated(this);
}
protected void onResume() {
super.onResume();
UIController.getInstance().onActivityResumed(this);
}
}

public class ActivityB() {
// same as ActivityA
}

public class ActivityC() {
// same as ActivityA
}

如果有人有更好的解决方案,请随时发布。

最佳答案

听起来您应该将其设置为 true 并让 Android 处理 Activity 堆栈的管理。

android:alwaysRetainTaskState

If this attribute is set to "true" in the root activity of a task, the default behavior just described does not happen. The task retains all activities in its stack even after a long period.

关于java - 应用程序关闭后恢复 Android 返回堆栈,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6836234/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com