gpt4 book ai didi

java - 静态与非静态 Activity Intent 调用方法

转载 作者:行者123 更新时间:2023-11-30 10:46:08 26 4
gpt4 key购买 nike

在这里,我对静态 Intent 调用方法与直接 Intent 调用有点混淆。从内存的角度来看,创建新 Activity 的更好选择是什么?

据我所知,如果使用静态的 Intent 方法调用它包含应用程序生命周期的内存。是真是假?

让我们举个例子:

在 Activity B中

public static Intent newIntent(Context context) {
Intent intent = new Intent(context, B.class);
return intent;
}

从 Activity A 调用 Activity B

在 Activity A 中

 startActivity(B.newIntent(this));

以其他方式直接调用 Activity 不能在 finish() 调用 Activity 之后存活。对吧?

startActivity(new Intent(context, B.class));

我仍然认为从您的代码点和内存角度来看,第二个更好。但是我看到很多项目都包含 first(static calling) 方法。所以我想知道什么是调用新 Activity 的更好选择?

最佳答案

public static Intent newIntent() 方法是静态的,但仅此而已。这个静态方法的用途是你可以在没有B实例的情况下调用B.newIntent()

您传递给 B.newIntent(this) 的上下文不是静态的,因此无论您是在 A 还是在 B 中创建 Intent 都没有关系

A 中的这个

startActivity(B.newIntent(this));

在A中和这个没什么区别

startActivity(newIntent(this));

private Intent newIntent(Context context) {
Intent intent = new Intent(context, B.class);
return intent;
}

So I want to know what is better selection to calling new Activity?

在功能上它没有区别。如果内存使用有任何差异,它会非常小,您不会注意到。

就编码风格而言,最好将 Intent 的创建保留在 A 中,因为 A 正在启动 BB 不应该关心 A 如何处理它。

关于java - 静态与非静态 Activity Intent 调用方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36716213/

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