简体   繁体   中英

Application brought back to foreground with wrong activity

I have a problem with my application when it is brought back to the foreground in the case the phone went low on memory while the application was hidden:

The class inheriting from Application is re-created ( onCreate is called again), thus losing data it held before. The Activity which is restarted is not the one tagged as main action in the Manifest, but the last one that was active. This is a problem as the main activity, from which the user logs in, is the one responsible for filling in the Application subclass' data and I can't fill it in later.

Is there any way to tell the application to restart at the main activity instead of the latest one in this case ?

You could check in onResume() if the user is logged in. Means that you check if your Application data is filled. If this is not the case, finish the activity and start your first Application.

The user expect to return to the latest activity, so a general "always start first activity" would upset the user...

you could keep the data by implementing methods such as onPause() and onResume() Check this link out: http://developer.android.com/reference/android/app/Activity.html

this answer might also be helpful: onSaveInstanceState () and onRestoreInstanceState ()

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