Android后退按钮新意图或finish()

发布于 2024-12-09 14:01:24 字数 369 浏览 4 评论 0原文

我有一个问题。我有一个列表视图 (A) 和一个详细信息视图 (B)。

在 B 上,我有一个“查看列表”按钮,始终将用户带到列表。问题是,我可以通过通知到达 B,而不必从 A 到达。

因此,当单击 B 时,我不能只使用 finish(),因为我可能是从通知进入的,所以我可能需要开始一个新的某些情况下的活动。

我如何可靠地判断我是来自 A 还是来自通知,以便我可以采取正确的操作。

除此之外,这是我应该担心的事情吗?或者每次都开始一个活动就可以了。在我在A->之间跳跃的情况下B-> A-> B-> A-> B 一遍又一遍...从来没有调用过 finish()...这会减慢速度吗?

谢谢,

杰拉德。

I have a question. I have a list view (A) and a details view (B).

On B, I have a "view list" button that will always take the user to the list. The thing is, I can get to B via a notification and not necessarily from A.

So when clicking B, I can't just use finish() because I could have come in from a notification, so I may need to start a new activity in some cases.

How do I reliably tell if I came in from A or from a notification so that I can take the right action.

Added to that, is this something I should be worrying about? Or is it ok just to start an activity every time. In the case where I bouncing between A -> B -> A -> B -> A -> B over and over... never once calling finish()... will that slow things down?

Thanks,

Gerard.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

久光 2024-12-16 14:01:24

当您以意图启动 Activity B 时,添加一个 extra,然后检查 Activity B 中 extra 的状态。这是一个使用字符串的示例,尽管您可以使用其他变量类型(如布尔值或 int):

Intent i = new Intent(this, ActivityB.class)
i.putExtra("startedBy", "ActivityA"); 

然后在 Activity B 中您可以获得额外变量:

Bundle extras = this.getIntent().getExtras(); 
String startedBy = null;

if (extras != null) {
    startedBy = extras.getString("startedBy");
}

When you start Activity B with an intent add an extra and then check the state of extra in activity B. Here is an example using strings although you could use other variable types like a boolean or an int:

Intent i = new Intent(this, ActivityB.class)
i.putExtra("startedBy", "ActivityA"); 

Then in Activity B you can get the extra variable with:

Bundle extras = this.getIntent().getExtras(); 
String startedBy = null;

if (extras != null) {
    startedBy = extras.getString("startedBy");
}
浪漫人生路 2024-12-16 14:01:24

当你从 A -> 进入时,你可以添加一个意图额外标志。 B,说一个布尔标志。
在活动 B 上,如果它不携带额外的意图,您可以将标志默认为 false。

You can add an intent extra flag when you go from A -> B, say a boolean flag.
On Activity B, you can default the flag to false if it doesn't carry the intent extra.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文