android 摘抄,from developers

本文详细解析了Android中Activity的生命周期,特别是当在onCreate()方法中调用finish()时,系统将不会调用onPause()和onStop(),而是直接进入onDestroy()阶段。此外,文章还介绍了不同情况下Activity实例的状态保存与恢复机制。

当在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/).

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值