一、activity生命周期
1、activity的4中状态
running/paused/stoped/killed
2、activity的生命周期
//--------------单个A启动---------------------
A启动: onCreate---->onStart---->onResume 处于running状态
点击HOME键: onPause---->onStop 处于stoped状态
点击桌面程序启动: onRestart---->onStart---->onResume 处于running状态
退出A: onPause---->onStop---->onDestroy 处于killed状态
//--------------A启动B----------------------
A启动: onCreate(A)---->onStart(A)---->onResume(A) A处于running状态
A启动B: onResume(A)---->onPause(A) A先处于paused状态
onCreate(B)---->onStart(B)---->onResume(B) B启动处于running状态
onPause(A)---->onStop(A) A处于stoped状态
B退出: onResume(B)---->onPause(B) B先处于paused状态
onRestart(A)---->onStart(A)---->onResume(A) A启动处于running状态
onPause(B)---->onStop(B)---->onDestroy(B) B处于killed状态
退出A: onPause(A)---->onStop(A)---->onDestroy(A A处于killed状态
注意:
onPause的意思是另一个activity跑到前台来,前一个activity的onPuse会被调用。
弹出AlertDialog、Toast或下拉通知栏虽然也让当前Activity处于不可交互的状态但对Activity的生命周期不会产生任何影响,该Activity依然处于running状态,而不是paused状态。
当将activity设置为对话框样式时:
android:theme="@style/Theme.AppCompat.Dialog"
或者将activity设置为透明时:
android:theme="@android:style/Theme.Translucent"
android:theme="@android:style/Theme.Translucent.NoTitleBar"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
是会调用onPause方法的。不会调用onStop方法
3、activity进程优先级
前台>可见>服务>后台>空
前台进程包括:
1、处于running状态的Activity
2、与处于running状态的Activity绑定的service
3、前台服务(服务调用了startForeground())
4、Service 正在执行onCreate(), onStart(), or onDestroy()方法
5、BroadcastReceiver 正在执行 onReceive() 方法
可见进程包括:
1、 Activity 不在前端显示 , 但也没有完全隐藏,能够看得见
2、一种绑定到可见activity的service
服务进程:
普通的service 即A process that is running a service that has been started with the startService() method
后台进程:
A process holding an activity that's not currently visible to the user (the activity's onStop() method has been called)
空进程:
没有运行任何Components的进程,保留这个进程主要是为了缓存的需要。空进程是为了做缓存,内存里记录一些值,下次打开快。
二、activity任务栈
一个任务是用户在执行某种工作时所需要的activities的集合。
当前的activity启动了另一个activity,新的activity被放置在栈顶并拥有焦点.先前的activity依然保存在栈中,但是停止了.当一个activity停止时,系统保存了它的用户界面的当前状态.当用户点击后退按钮时,当前的activity被从栈顶弹出(activity被销毁了)并且先前的activity被恢复了.栈中的activities永不会被重新排列,只会进行入栈或出栈行为—当被当前activity启动时就入栈,当用户使用后退按钮离开它时就出栈.如此,后退栈也是一个后进先出的栈
三、activity启动模式 与任务栈相关联
1、standard 每次都启动一个新的实例,处于同一任务栈中
2、singletop 栈顶复用机制.如果任务栈的顶端存在改实例就复用,否则就新建 onNewIntent
3、singletask 如果任务栈中已经存在改实例,就复用并且将该实例之上的实例全部移除 onNewIntent
4、singleinstance 创建一个新的任务栈存放该实例,并保证该任务栈中只有唯一的实例
四、scheme跳转协议
Android中的Scheme是一种页面内跳转协议,通过自定义Scheme协议,可以跳转到app中的任何页面。
服务器可以定制化跳转app页面app可以通过Scheme跳转到另一个app页面可以通过h5页面跳转app原生页面
使用:
定义一个Scheme
<activity>
<!-- 给页面添加指定的过滤器-->
<intent-filter>
<!--该页面的路径配置-->
<data android:host="test" android:path="/goods" android:port="8080" android:scheme="qh">
<!--下面这几行也必须得设置-->
<category android:name="android.intent.category.DEFAULT">
<category android:name="android.intent.category.BROWSABLE">
</category></action></category></data></intent-filter>
</activity>
调用方式
-
原生调用
Intent intent1 = new Intent(Intent.ACTION_VIEW, Uri.parse("qh://test:8080/goods?goodsId=8897&name=fuck")); startActivity(intent1);
-
html调用
<a href="qh://test:8080/goods?goodsId=8897&name=fuck">打开商品详情</a>
判断某个Scheme是否有效
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("qh://test:8080/goodsgoodsId=8897&name=fuck"));
List<resolveinfo> activities = getPackageManager().queryIntentActivities(intent, 0);
boolean isValid = !activities.isEmpty();
if (isValid) {
startActivity(intent);
}
获取scheme并解析代码
Uri uri = getIntent().getData();
if (uri != null) {
//dosomething
}