简体   繁体   English

Android - 活动娱乐期间是否保留了意图?

[英]Android - Is the intent preserved during activity recreation?

I am building an android application, and would like to ask if the intent that started an activity (which is reachable via the getIntent () method) is saved / preserved during activity recreation such as a device orientation, or simply the fact that android can erase the application state if it is in the background and rebuild it (if it is low on memory) ? 我正在构建一个Android应用程序,并且想询问是否在活动重新创建期间(例如设备方向)保存/保存了启动活动的意图(可通过getIntent()方法访问),或者只是知道android可以擦除应用程序状态,如果它在后台并重建它(如果内存不足)?

This is a quick example that illustrates the question : 这是一个简单的例子,说明了这个问题:

The application launches activity A . 该应用程序启动活动A. Then the user clicks on a button that starts a new activity B . 然后,用户单击启动新活动B的按钮。 Activity A has sent to activity B a string X with the value Hello via the intent (using the putExtra method). 活动A通过意图(使用putExtra方法)向活动B发送了一个字符串X ,其值为Hello

In activity B , I can retrieve the content of string X by retrieving the intent (via the getIntent () method) and then retrieving the string content (via the getStringExtra method). 在活动B中 ,我可以通过检索intent(通过getIntent()方法)然后检索字符串内容(通过getStringExtra方法)来检索字符串X的内容。

Will I still be able to retrieve the extra string from intent, or even the intent itself if the activity is recreated due to device rotation, ... ? 如果由于设备轮换而重新创建活动,我是否仍然可以从intent中检索额外的字符串,甚至是意图本身... ...?

Or should I save the extra string in the onSaveInstanceState method ? 或者我应该在onSaveInstanceState方法中保存额外的字符串?

I have tried the device rotation scenario, and the intent (along with the extra string) are always accessible. 我已经尝试过设备旋转场景,并且始终可以访问intent(以及额外的字符串)。

Will I still be able to retrieve the extra string from intent, or even the intent itself if the activity is recreated due to device rotation 如果由于设备旋转而重新创建活动,我是否仍然能够从intent中检索额外的字符串,甚至是intent本身

Yes. 是。 You will have the same Intent (or, at least, a copy of the Intent ) after the configuration change as you had before the configuration change. 在配置更改之后,您将具有与配置更改之前相同的Intent (或至少是Intent的副本)。

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

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