对Activity的一些简单的理解

Activity的子类极其作用*

*

Activity的生命周期:

**
onCreate:第一个方法,做一些初始化的工作
onStart:Activity正在启动,可见,但无法和用户进行交互
onRestoreInstanceState在这里进行状态信息的还原
onResume:可见并且用户可以进行操作
onPause:Activity正在停止
第一个Activity的onSaveInstanceState在这里执行
第二个Activity的onCreate,onStart,onResume在这个阶段执行。
onStop:Activity即将停止
onRestart:Activity被重新启动,执行完后从onStart开始执行
onDestory:马上要被销毁
onStop和onPause中不能进行耗时操作.
onSaveInstanceState保存状态信息y
onRestoreInstanceState在这里进行状态信息的还原

关于旋转屏幕重建的问题:

在midSdkVersion和targetSdkVersion均小于13时,设置android:configChanges“orientation”这一个属性,屏幕旋转时就不会被重建。
但是当midSdkVersion或targetSdkVersion有一个大于13时,就要设置为
android:configChanges=”orientation|screenSize”
才能使旋转屏幕时不会进行Activity重建

Activity的四种启动模式

1.standard:标准模式
*特点:每启动一次Activity创建一个实例,不管该实例是否已经存在,并且该实例存放在启动它的Activity的任务栈中,比如B被A启动了,B启动模式为standard,那么B的实例就在A的任务栈中。*
2.singleTop:栈顶复用
特点:如果被调用的Activity处于任务栈栈顶,那么它不会被重建,同时onNewIntent会被调用,这里我把onNewIntent的源码贴上来

/**
     * This is called for activities that set launchMode to "singleTop" in
     * their package, or if a client used the {@link Intent#FLAG_ACTIVITY_SINGLE_TOP}
     * flag when calling {@link #startActivity}.  In either case, when the
     * activity is re-launched while at the top of the activity stack instead
     * of a new instance of the activity being started, onNewIntent() will be
     * called on the existing instance with the Intent that was used to
     * re-launch it.
     *
     * <p>An activity will always be paused before receiving a new intent, so
     * you can count on {@link #onResume} being called after this method.
     *
     * <p>Note that {@link #getIntent} still returns the original Intent.  You
     * can use {@link #setIntent} to update it to this new Intent.
     *
     * @param intent The new intent that was started for the activity.
     *
     * @see #getIntent
     * @see #setIntent
     * @see #onResume
     */

    protected void onNewIntent(Intent intent) {
    }

源码中给的注释大概的意思就是在Activity的启动模式为singTop时,被调用。
如果该Activity在栈顶,onNewIntent会去请求已经存在的Intent实例,并且重新启动该Activity,此时该Activity不会再执行onCreate,和onStart。
3.singleTask 栈内复用:只要Activity的任务栈中存在该实例,那么它就不会被重建。
当一个模式为singTask的Activity被启动以后会在任务栈中进行寻找,是否存在Activity所需的任务栈,而且该任务栈中是否有该Activity,如果有,并且该Activity不在栈顶,那么在该Activity上的所有Activity都会出栈。例如:栈内顺序为ABCD现在我们的Activity是B,栈内存在,那么就不创建新实例,而是将CD出栈,,直接用B,接下来就和singTop一样,也会调用onNewIntent
4singleInstance 单实例模式:这个模式的下每个Activity独占一个任务栈

/**
* The affinity this activity has for another task in the system. The
* string here is the name of the task, often the package name of the
* overall package. If null, the activity has no affinity. Set from the
* {@link android.R.attr#taskAffinity} attribute.
*/
public String taskAffinity;
“`

任务栈:上边的taskAffinity标识了任务栈的名字。
设置启动方式有两种,一种是在androidmanifest进行launchMode属性进行设置
另一种是在Intent中设置addFlags来设置

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值