0

I started reading around about the activity life cycle callbacks and saving state and there are quite a few things I don't understand - I'm writing an android app but I want to ask more general questions than how to do it specifically for the few activities etc I have at the moment, I would like to have a better overall view of how this works!

There are two groups of methods I have seen being used (I have seen one or two others but don't want to confuse myself even further...)

  • onPause, onResume etc,
  • and then the onSaveInstanceState ones.

What is the difference between them and the circumstances we should be looking to use them? I have seen some questions where a poster is using one of the normal life cycle callbacks, and is told to use onSaveInstanceState instead, so when should we be implementing onPause rather than onSaveInstanceState and so on. Some posts mentioned about methods being used for transient state only, could someone expand on that?

I have seen state being used to mean slightly different things - UI/View state and Activity state, what is the difference between the two?
I am also a bit unsure with what they mean by state, when we are saving state what kind of things are we saving exactly, could anyone give some quick examples (I don't mean actual code)? The android developer guides say that the android system automatically takes care of some of this, so what should we be concerned with? Bundle objects used by onCreate and onSaveInstanceState only store simple values, so what about more complex objects and arrays.

Thanks

4

3 回答 3

0

尝试使用此代码保存状态

@Override
protected void onSaveInstanceState(Bundle outState) {
  State s = new State(yourTextView.getText().toString());
  outState.putSerializable(State.STATE, s);
  super.onSaveInstanceState(outState);
}

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
  super.onRestoreInstanceState(savedInstanceState);
  State s = (State) savedInstanceState.getSerializable(State.STATE);
  yourTextView.setText(s.getYourTextViewText());
}
于 2013-02-27T17:15:11.310 回答
0

Android 可以在任何给定时间破坏您的活动,甚至终止您的进程(尽管它对用户可见时不太可能:-))。当用户导航回活动时,应该再次显示在他或她离开之前显示在屏幕上的数据/信息。

onSaveInstanceState回调允许您执行此操作。

大多数视图已经自动为您执行此操作。例如,EditText 中的当前文本、ListView 的当前滚动位置等都会自动为您保存。

但是,有些内容不会自动为您保存。例如,TextView 中的当前文本,特定视图的(更改的)背景可绘制对象。

假设您在用户操作失败后显示错误消息。然后错误消息显示在 a 中TextField,并且 thisTextField的背景变为红色(我只是在这里编造这个 :-))。当用户在显示此错误时离开活动(例如按下主页按钮),活动被销毁,当用户返回活动时,错误消息和红色背景将不再显示。

这就是onSaveInstanceState救援的地方。
您可以String在其中保存包含错误消息的内容。然后在重新创建活动时,Bundle savedInstanceStateonCreate为空,您可以查询它以获取错误消息。如果此消息不为空/空,请调用错误消息并将其setText背景设为红色。TextViewTextView

于 2013-02-27T17:28:32.983 回答
0
protected void onPause ()
protected void onSaveInstanceState (Bundle outState)

只看它,onSaveInstanceState 有一个 Bundle,你可以把你需要保存的东西放进去。并在 onCreate(Bundle) 或 onRestoreInstanceState(Bundle) 中取回它;文档中的一些重要行:

这个方法在一个活动可能被杀死之前被调用,这样当它在未来某个时间回来时它可以恢复它的状态。不要将此方法与活动生命周期回调混淆,例如 onPause(),它总是在活动被放置在后台或销毁的途中调用,或者 onStop() 在销毁之前调用。

于 2013-02-27T17:02:26.500 回答