本文共 2704 字,大约阅读时间需要 9 分钟。
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. * *An activity will always be paused before receiving a new intent, so * you can count on {@link #onResume} being called after this method. * *
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一样,也会调用onNewIntent4singleInstance 单实例模式:这个模式的下每个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来设置转载地址:https://blog.csdn.net/mengmengkenanjun/article/details/51307932 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!