简体   繁体   English

保存Android应用程序状态

[英]Saving Android application state

I understand how to save an application's state by using SharedPreferences, onSavedInstanceState() & onRestoreInstanceState(), etc as outlined in a similar post ( Saving Android Activity state using Save Instance State ), but how do I save the last activity? 我了解如何通过使用SharedPreferences,onSavedInstanceState()和onRestoreInstanceState()等来保存应用程序的状态,如类似帖子( 使用保存实例状态保存Android活动状态 )中所述,但如何保存上一个活动?

To be more specific, my application starts up and goes to a login screen. 更具体地说,我的应用程序启动并进入登录屏幕。 Once a user logs in and navigates through several activities, lets say he or she leaves the app using the home button or in some other way. 一旦用户登录并浏览了几个活动,就可以说他或她使用主页按钮或以其他方式离开应用程序。 Next time the user starts the app, it will go back to the login screen and do a login again. 下次用户启动应用程序时,它将返回登录屏幕并再次登录。 Instead, I want the app to start up and go to the last activity that was on top of the stack when the user left the app in the previous session. 相反,我希望应用程序启动并转到用户在上一个会话中离开应用程序时在堆栈顶部的最后一个活动。

How is the last activity saved so that it can be restored on app startup? 如何保存最后一个活动,以便可以在应用启动时恢复?

I believe Android does this automatically. 我相信Android会自动执行此操作。 We have an app that we are working on. 我们正在开发一款应用。 When I click the home button and them come back to the app, it starts in the activity where I left off. 当我点击主页按钮然后他们回到应用程序时,它会从我离开的活动开始。 We have not written any code to make this happen. 我们还没有编写任何代码来实现这一目标。 It just seems to work. 它似乎工作。

My colleague has written an article on Android application state including details on getLastNonConfigurationInstance() which retrieves the instance that was last stored. 我的同事写了一篇关于Android应用程序状态的文章,其中包括getLastNonConfigurationInstance()的详细信息,该文件检索上次存储的实例。 Take a look here: http://www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx 看看这里: http//www.eigo.co.uk/Managing-State-in-an-Android-Activity.aspx

Please note that the above answer is correct only on one case: If your process does not get killed by android because the resources (memory, ...) are needed for a different reason. 请注意,上述答案仅在一种情况下是正确的:如果你的进程没有被android杀死,因为资源(内存,......)是出于不同原因需要的。

To get what you describe, I would write a custom parent activity and override the correct life-cycle methods to store your application state and read it and act accordingly. 为了得到你描述的内容,我会编写一个自定义父活动并覆盖正确的生命周期方法来存储你的应用程序状态并阅读它并采取相应的行动。 Then let all your activities inherit from MyActivity (instead of android.app.Activity) 然后让你的所有活动都继承自MyActivity(而不是android.app.Activity)


public MyActivity extends android.app.Activity {
...
@Override
public onCreate(...) {
  // Read the Application state, 
  // check if the currently launching Activity is the right one, 
  // if not, start the last Activity and finish the current one.
}

@Override
public onDestroy(...) {
  // Store the current Activity ID in the SharedPreferences
}
...
}

Take care to call the super.onDestroy and super.onCreate methods in all your Activites (like you should do anyways). 注意在所有Activites中调用super.onDestroy和super.onCreate方法(就像你应该做的那样)。

Happy coding and have fun with Android! 快乐编码,玩得开心!

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

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