android中finish()和onDestroy()的关系
来源:互联网 发布:雷士照明淘宝旗舰店 编辑:程序博客网 时间:2024/06/02 02:57
一、activity的onDestroy()函数
Google解释:
/**
* Perform any final cleanup before an activity is destroyed. This can
* happen either because the activity is finishing (someone called
* {@link #finish} on it, or because the system is temporarily destroying
* this instance of the activity to save space. You can distinguish
* between these two scenarios with the {@link #isFinishing} method.
*
* <p><em>Note: do not count on this method being called as a place for
* saving data! For example, if an activity is editing data in a content
* provider, those edits should be committed in either {@link #onPause} or
* {@link #onSaveInstanceState}, not here.</em> This method is usually implemented to
* free resources like threads that are associated with an activity, so
* that a destroyed activity does not leave such things around while the
* rest of its application is still running. There are situations where
* the system will simply kill the activity's hosting process without
* calling this method (or any others) in it, so it should not be used to
* do things that are intended to remain around after the process goes
* away.
*
* <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 #onPause
* @see #onStop
* @see #finish
* @see #isFinishing
*/
说白了onDestroy就是释放资源作用的。
源代码:
protected void onDestroy() {
mCalled = true;
// dismiss any dialogs we are managing.
if (mManagedDialogs != null) {
final int numDialogs = mManagedDialogs.size();
for (int i = 0; i < numDialogs; i++) {
final ManagedDialog md = mManagedDialogs.valueAt(i);
if (md.mDialog.isShowing()) {
md.mDialog.dismiss();
}
}
mManagedDialogs = null;
}
// close any cursors we are managing.
synchronized (mManagedCursors) {
int numCursors = mManagedCursors.size();
for (int i = 0; i < numCursors; i++) {
ManagedCursor c = mManagedCursors.get(i);
if (c != null) {
c.mCursor.close();
}
}
mManagedCursors.clear();
}
// Close any open search dialog
if (mSearchManager != null) {
mSearchManager.stopSearch();
}
getApplication().dispatchActivityDestroyed(this);
}
可以看见在调用 getApplication().dispatchActivityDestroyed(this)之前,有三个for循环,分别释放了activity中的dialogs,cursors 和 search dialog。最后调用Application的dispatchActivityDestroyed(this)。我们再来看看这个函数
dispatchActivityDestroyed()源码:
/* package */ void dispatchActivityDestroyed(Activity activity) {
Object[] callbacks = collectActivityLifecycleCallbacks();
if (callbacks != null) {
for (int i=0; i<callbacks.length; i++) {
((ActivityLifecycleCallbacks)callbacks[i]).onActivityDestroyed(activity);
}
}
}
越来越深,以后再研究吧
二、finish()
/**
* Call this when your activity is done and should be closed. The
* ActivityResult is propagated back to whoever launched you via
* onActivityResult().
*/
public void finish() {
if (mParent == null) {
int resultCode;
Intent resultData;
synchronized (this) {
resultCode = mResultCode;
resultData = mResultData;
}
if (false) Log.v(TAG, "Finishing self: token=" + mToken);
try {
if (resultData != null) {
resultData.setAllowFds(false);
}
if (ActivityManagerNative.getDefault()
.finishActivity(mToken, resultCode, resultData)) {
mFinished = true;
}
} catch (RemoteException e) {
// Empty
}
} else {
mParent.finishFromChild(this);
}
}
/**
* This is called when a child activity of this one calls its
* {@link #finish} method. The default implementation simply calls
* finish() on this activity (the parent), finishing the entire group.
*
* @param child The activity making the call.
*
* @see #finish
*/
public void finishFromChild(Activity child) {
finish();
}
- android中finish()和onDestroy()的关系
- Android :Activity中finish() onDestroy() 和System.exit()的区别
- Android :Activity中finish() onDestroy() 和System.exit()的区别
- Activity中finish()和onDestroy()的区别
- Activity中finish()和onDestroy()的区别
- Activity中finish()和onDestroy()的区别
- Activity中finish()和onDestroy()的区别
- Activity中finish()和onDestroy()的区别
- Activity中finish()和onDestroy()的区别
- Activity中finish()和onDestroy()的区别
- Activity中finish()和onDestroy()的区别
- Android中 finish()与onDestroy()
- finish 和 onDestroy 的区域
- android中常用的finish()与onDestroy()的区别
- [感谢大佬之Android] Activity中finish()和onDestroy()的区别
- (转)Android开发-Activity中finish() onDestroy() 和System.exit()的区别
- android activity 的finish和ondestroy的区别
- android开发中finish()、onDestroy()、System.exit(0)的区别
- gbk转utf8
- hlist_head
- Twitter开源云环境时间序列数据断层检测工具BreakoutDetection
- java calendar
- Javascript模块化编程(二):AMD规范
- android中finish()和onDestroy()的关系
- 我是一只修理猫-2
- android 修改DatePickerDialog只显示“年月”,去掉“日”显示
- Android--Spinner(下拉列表效果)
- ASIHTTPRequest的startAsynchronous调用EXC_BAD_ACCESS
- 实现通知中心/控制中心等模糊背景的图层
- LeetCode: Search a 2D Matrix
- if __name__ == "__main__":作用
- 我的IT之路,学c++ ~1++