当在onCreate() 中 finish() 当前actiivty,系统会 越过 onPause() ,onStop() 直接调用onDestory() .
原文如下,摘自developers:
Note: The system calls onDestroy()
after it has already calledonPause()
andonStop()
in all situations except one: when you callfinish()
from within theonCreate()
method. In some cases, such as when your activity operates as a temporary decision maker tolaunch another activity, you might callfinish()
from withinonCreate()
to destroy the activity. In this case, the systemimmediately callsonDestroy()
without calling any of the otherlifecycle methods.
When your activity is destroyed because the user pressesBack or the activity finishesitself, the system's concept of thatActivity
instance is gone forever becausethe behavior indicates the activity is no longer needed. However, if the system destroysthe activity due to system constraints (rather than normal app behavior), then although the actualActivity
instance is gone, the system remembers that it existed such that ifthe user navigates back to it, the system creates a new instance of the activity using a set ofsaved data that describes the state of the activity when it was destroyed.
Instead of restoring the state during onCreate()
youmay choose to implementonRestoreInstanceState()
, which the system callsafter theonStart()
method. The system calls onRestoreInstanceState()
only if there is a savedstate to restore, so you do not need to check whether theBundle
is null:
When you remove or replace a fragment and add the transactionto the back stack, the fragment that is removed is stopped (not destroyed). If the user navigatesback to restore the fragment, it restarts. If youdo not add the transaction to the backstack, then the fragment is destroyed when removed or replaced.
android中可以在xml 文件中放心使用高版本属性,原因如下:
When parsing XML resources, Android ignores XML attributes that aren’t supported by the current device. So you can safely use XML attributes thatare only supported by newer versions without worrying about older versions breaking when theyencounter that code. For example, if you set the targetSdkVersion="11"
, your app includes the ActionBar
by defaulton Android 3.0 and higher. To then add menu items to the action bar, you need to set android:showAsAction="ifRoom"
in your menu resource XML. It's safe to do this in a cross-version XML file, because the older versions of Android simply ignore the showAsAction
attribute (that is, you do not need a separate version in res/menu-v11/
).