我知道这个问题必须有几十个答案,但要么我找不到,要么我不理解。
问题:
如何让我的应用程序完全按照剩下的方式启动? F.E.动态添加的复选框不应该消失!
答案 0 :(得分:1)
您可以在SharedPreferences中存储此类值。
https://developer.android.com/training/basics/data-storage/shared-preferences.html
使用键值方法进行保存。因此,您可以保存一些值,并随时从SharedPreferences中读取它。
这是小数据的最佳方法,可以在应用程序启动时使用。因此,您可以退出应用程序并且数据仍然存在 - 因此可以在下次应用程序启动时阅读。
答案 1 :(得分:1)
或者将程序的条件保存到文本文件中,以便程序可以在它停止之前将其“转换”回条件,或者我不建议,它会保存使用ObjectOutputStream创建的每个对象。
答案 2 :(得分:1)
没有“开箱即用”的方式。您可以通过某种方式保存活动的当前状态(More on persistence)
然后,您需要能够在Activity生命周期
中重建所需的持久状态您可以使用共享首选项保存和加载,例如:
public void saveState(YourState state) {
SharedPreferences sharedPreferences = app.getSharedPreferences(R.string.preference_file_key, Context.MODE_PRIVATE)
sharedPreferences.edit()
.putString("CustomAtt", state.getCustomAtt())
}
public YourState loadState() {
SharedPreferences sharedPreferences = app.getSharedPreferences(R.string.preference_file_key, Context.MODE_PRIVATE)
String customAtt = sharedPreferences.getString("CustomAtt", "DefaultValue")
return new YoutState(customAtt)
}
并像这样使用
@Override
protected void onCreate(Bundle savedInstanceState) {
YourState state = loadState();
// Rebuild your activity based on state
someView.setText(state.getCustomAtt())
}