简体   繁体   English

浏览多个活动

[英]Navigating through multiple activities

I have 2 Activities A and B . 我有2个活动AB。 Now these are my objectives. 现在这些是我的目标。

  1. When I'm in B and if I press the Home button, the state of the Activity should be saved. 当我在B中并且如果按“ 主页”按钮时,应保存“活动”的状态。 (No problem with this.) (这没问题。)
  2. When I start B from A after step 1 a new instance of B should be created (ie) Previous state should be discarded. 当我在第1步之后从A开始B时 ,应创建B的新实例(即,应该放弃先前的状态)。

But in Step 2 the state of B still prevails. 但是在步骤2中,状态B仍然有效。 How do I accomplish my objective? 我如何实现我的目标?

I think one possible solution woudl be to pass some extra information inside the starting Intent, when you start Activity B from A (like a boolean value). 我认为一种可行的解决方案是,当您从A启动Activity B时,在启动Intent内传递一些额外的信息(如布尔值)。 And in the "onStart()" of B, you check if you can find this extra info in the intent (you get it with getIntent()). 然后在B的“ onStart()”中,检查是否可以在意图中找到此额外信息(您可以通过getIntent()获得它)。 If it's not present, that means you do reload the activity's previous state. 如果不存在,则表示您确实要重新加载活动的先前状态。 If it is, then you don't reload it. 如果是这样,则您无需重新加载它。

You don't even need to send a boolean like Scythe suggested. 您甚至不需要发送像Scythe建议的布尔值。 The Bundle savedInstanceState will be null in onCreate for Activity B if Activity A just started it, whereas it will be non-null if you are coming back from a saved state. 如果活动A刚刚开始,则在活动B的onCreate Bundle savedInstanceState将为null,而如果您从已保存状态返回,则该Bundle savedInstanceState将为非null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM