首页 > 代码库 > Activity重要函数

Activity重要函数

一。onConfigurationChanged 与 android:configChanges

Lists configuration changes that the activity will handle itself. When a configuration change occurs at runtime, the activity is shut

down and restarted by default, but declaring a configuration with this attribute will prevent the activity from being restarted.

Instead, the activity remains running and its onConfigurationChanged() method is called.

配置了android:configChanges后,当声明的configChange改变时,系统会调用Activity的onConfigurationChanged,而不会重新创建Activity.

android:configChanges="orientation|keyboardHidden|screenSize"

HONEYCOMB_MR2(3.2以上) 

 

二。onRetainNonConfigurationInstance 与 getLastNonConfigurationInstance

This function is called purely as an optimization, and you must not rely on it being called. When it is called, a number of guarantees

will be made to help optimize configuration switching:

  • The function will be called between onStop() and onDestroy().
  • A new instance of the activity will always be immediately created after this one‘s onDestroy() is called. In particular, no messages

will be dispatched during this time (when the returned object does not have an activity to be associated with).

  • The object you return here will always be available from the getLastNonConfigurationInstance() method of the following activity

instance as described there.

这个方法最大的好处是: 
    * 当Activity曾经通过某个资源得到一些图片或者信息,那么当再次恢复后,无需重新通过原始资源地址获取,可以快速的加载整个Activity状态信息。 

    * 当Activity包含有许多线程时,在变化后依然可以持有原有线程,无需通过重新创建进程恢复原有状态。 

    * 当Activity包含某些Connection Instance时,同样可以在整个变化过程中保持连接状态。 

注意: 
    * onRetainNonConfigurationInstance()在onSaveInstanceState()之后被调用。 

    * 调用顺序同样介于onStop() 和 onDestroy()之间。

 

三。Fragment 的 setRetainInstance

Control whether a fragment instance is retained across Activity re-creation (such as from a configuration change). This can only be

used with fragments not in the back stack. If set, the fragment lifecycle will be slightly different when an activity is recreated:

  • onDestroy() will not be called (but onDetach() still will be, because the fragment is being detached from its current activity).
  • onCreate(Bundle) will not be called since the fragment is not being re-created.
  • onAttach(Activity) and onActivityCreated(Bundle)will still be called.

 

四。onSaveInstanceState 与 onRestoreInstanceState

This method is called before an activity may be killed so that when it comes back some time in the future it can restore its state.

For example, if activity B is launched in front of activity A, and at some point activity A is killed to reclaim resources, activity A will

have a chance to save the current state of its user interface via this method so that when the user returns to activity A, the state

of the user interface can be restored via onCreate(Bundle) oronRestoreInstanceState(Bundle).

The default implementation takes care of most of the UI per-instance state for you by calling onSaveInstanceState() on each view in

the hierarchy that has an id, and by saving the id of the currently focused view. If you override this method to save additional

information not captured by each individual view, you will likely want to call through to the default implementation, otherwise be

prepared to save all of the state of each view yourself.

If called, this method will occur before onStop(). There are no guarantees about whether it will occur before or after onPause().