简体   繁体   中英

Activity is destroyed on intent creation

I'm writing an application implementing a bar code scanner. Several values are gathered and displayed in the same Activity. Everytime the user wants to get a new value, he calls an Intent on a another application in the device which handles the bar code scanning part, and returns a String containing the actual value held by the bar code. In the end, mutliple values from multiple scans are meant to be displayed in the same Activity.

It works perfectly on two different devices (Nexus 7 on Android 4.4.2, Vuzix M100 on Android 4.0.4). On those two, it goes well and on second intent return, my previous values are still stored in the Activity. But on the Alcatel One Touch Mini s 2 (Android 4.3), everytime I come back to the first activity, it goes through the onCreate method of the relevant Activity and erases the previous scan values stored in global Activity variables.

I have searched over the web but I couldn't manage to find any clue. Why does only one phone out of three erases the Activity for every intent created, and how can I prevent it?

It doesn't seem to be a hardware ressource problem (as said in the doc, Paused intents can be destroyed to free memory) because the problematic device is almost the most powerful.

“每次回到第一个活动时,它都会通过相关活动的onCreate方法,并删除存储在全局活动变量中的先前扫描值”->是否可以将它们存储在每次在onCreate中读取的共享首选项中?

You need to be prepared for cases when your activity gets destroyed, and later recreated. For this Android calls onSaveInstanceState() where you can store the information that you need in the Bundle provided. Then when the activity is recreated, onCreate() will be called with this Bundle passed as a parameter, so you can read your data from it. You can read more about this here => Saving Activity State

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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