gpt4 book ai didi

android - SingleInstance Activity 让我感到困惑

转载 作者:搜寻专家 更新时间:2023-11-01 07:37:44 25 4
gpt4 key购买 nike

一个 Activity 是一个单一的实例 Activity 。

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class A extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("A");
startActivity(new Intent(this, B.class));
}

@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
testLog("new Intent A");
}

private void testLog(String string) {
Log.d("test", string);
}

@Override
protected void onDestroy() {
super.onDestroy();
testLog("destroy A");
}
}

B Activity 是标准 Activity 。

在正常情况下A->B。用户action返回操作,B销毁。用户再次按下,A 销毁。没错。

但在另一种情况下:A->B。用户操作主页按钮。当用户重启任务时,android框架调用A的onNewIntent()

为什么?我的意思是 Activity 堆栈应该是这样的:

-B
-A

为什么android框架将 Intent 路由到A?

最佳答案

singleTopsingleTasksingleInstance 之间存在差异。阅读the documentation了解更多信息,但您确实要注意 singleInstance 的描述。

Only allow one instance of this activity to ever be running. This activity gets a unique task with only itself running in it; if it is ever launched again with the same Intent, then that task will be brought forward and its Activity.onNewIntent() method called. If this activity tries to start a new activity, that new activity will be launched in a separate task. See the Tasks and Back Stack document for more details about tasks.

这正是您所看到的并且应该是预期的行为。

关于android - SingleInstance Activity 让我感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8394582/

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