之前项目中用到观察者而模式来通知其他页面更新数据,当时就有个小疑问,当通知其他页面是,要通知的页面不是onPause状态么? onPause状态是表示什么意思呢? activity所有活动的数据都暂停了?静止不动了? 啊哈 至少我前面是这么理解的,但是我通知后数据也是能正常刷新,正常更新UI的,跟我之前理解的有点不一样呢?看源码了
/**
* Called as part of the activity lifecycle when an activity is going into
* the background, but has not (yet) been killed. The counterpart to
* {@link #onResume}.
*
* <p>When activity B is launched in front of activity A, this callback will
* be invoked on A. B will not be created until A's {@link #onPause} returns,
* so be sure to not do anything lengthy here.
*
* <p>This callback is mostly used for saving any persistent state the
* activity is editing, to present a "edit in place" model to the user and
* making sure nothing is lost if there are not enough resources to start
* the new activity without first killing this one. This is also a good
* place to do things like stop animations and other things that consume a
* noticeable amount of CPU in order to make the switch to the next activity
* as fast as possible, or to close resources that are exclusive access
* such as the camera.
*
* <p>In situations where the system needs more memory it may kill paused
* processes to reclaim resources. Because of this, you should be sure
* that all of your state is saved by the time you return from
* this function. In general {@link #onSaveInstanceState} is used to save
* per-instance state in the activity and this method is used to store
* global persistent data (in content providers, files, etc.)
*
* <p>After receiving this call you will usually receive a following call
* to {@link #onStop} (after the next activity has been resumed and
* displayed), however in some cases there will be a direct call back to
* {@link #onResume} without going through the stopped state.
*
* <p><em>Derived classes must call through to the super class's
* implementation of this method. If they do not, an exception will be
* thrown.</em></p>
*
* @see #onResume
* @see #onSaveInstanceState
* @see #onStop
*/
@CallSuper
protected void onPause() {
if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);
getApplication().dispatchActivityPaused(this);
mCalled = true;
}
<h2> 那么我们先整理下方法注释:</h2>
(翻译的不怎么好见谅,英文快还给老师了)
① 当Activity 即将进入后台,并且没有被杀死,与之相对应的是onResume。
②A调起B Activity 情况下 在A onPause()
返回之后才会执行B的onCcreate()
③该回调函数通常用来保存一些Acitivity正常编辑的持续状态,以此来呈献给用户原有的正在编辑的模式和在如果资源不足,必须在首先要杀死当前Acitivity情况,才可以启动一个新的Activity情况下来确保没有任何东西丢失,总之是保存一些状态数据信息啦。
④ 通常停止一些动画以及比较占用CPU资源的事,做这些就是为了尽快的切换到下一个Acitivity,或者关闭一些专用入口,如相机等。
⑤有时候系统需要更多的内存,它也会回杀死这个停下来的过程来回收一些资源。正是因为如此,你需要确保你需要保存的状态信息需要在这个方法回调之前完成。一般来说 onSaveInstanceState()
在Acitivity里保存实例的状态。onPause()
则是用来保存一些 全局持久性数据(如内容提供者,文件等等)。
⑥接收到调用这个方法的命令之后,接下来通常会受到调用onStop()的指令(在下一个Acitivity 调用onResume()并且显示之后),然而有些情况下,直接调用到 onResume()而不需要先进入停止状态。
⑦衍生类必须调用父类方法的实现,不这样做会抛出异常,其实就是必须调用[super onPause()];
从方法上看getApplication().dispatchActivityPaused(this);
再去Application里看dispatchActivityPaused(Activity activity);
方法
</br>
/* package */ void dispatchActivityPaused(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityPaused(activity);
}
}
}
<h2>OK再看回调接口</h2>
public interface ActivityLifecycleCallbacks {
void onActivityCreated(Activity activity, Bundle savedInstanceState);
void onActivityStarted(Activity activity);
void onActivityResumed(Activity activity);
void onActivityPaused(Activity activity);
void onActivityStopped(Activity activity);
void onActivitySaveInstanceState(Activity activity, Bundle outState);
void onActivityDestroyed(Activity activity);
}
<h2>擦嘞 没注释</h2>
好吧 那就不继续往下走了,看了下调用startActivity的源码,东西蛮多的,偷懒了下次继续补吧,暂时就按字面意思吧,就是暂停分发给Activity任务,说白了就是,不派给Activity活干了。