AndroID 中 ActivitylifecycleCallbacks的实例详解
以上就是使用ActivitylifecycleCallbacks的实例,代码注释写的很清楚大家可以参考下,
MyApplication如下:
package com.cc; import java.util.linkedList; import androID.app.Activity; import androID.app.Application; import androID.os.Bundle; /** * Demo示例: * ActivitylifecycleCallbacks使用示例 * * ActivitylifecycleCallbacks用于对应用中Activity的生命周期的追踪和回调 * * 使用ActivitylifecycleCallbacks可实现: * 1 判断App是否在后台运行 * 2 关闭该应用所有Activity * * * (1)判断App是否在后台运行 * 在该实现中采用activityCounter记录Activity的个数. * 通过对于activityCounter是否为0判断当前APP是否在前台运行. * * 从ActivitylifecycleCallbacks的实现方法中我们的惯性思维是: * 在ActivitylifecycleCallbacks的onActivityResumed()中activityCounter+1. * 在onActivityPaused()中activityCounter-1,* 但是这么对么? * * 先看一下界面跳转时两个Activity的生命周期 * 当从Activity A跳转到Activity B时,两个Activity的生命周期如下: * A.onPause()->B.onCreate()->B.onStart()-> B.onResume()-> A.onStop() * * 假若按照上述方式那么: * 在A启动后activityCounter=1; * 当从A跳转到B时在调用A.onPause()时执行:activityCounter-1=0; * 即该APP运行在后台. * 这当然是错的,两个界面切换的过程中APP当然是运行在前台的。 * * 如下修改: * 在onActivityStarted()中对于activityCounter+1 * 在onActivityStopped()中对于activityCounter-1 * 小结:注意Activity跳转时的生命周期 * * (2)关闭该应用所有Activity * 2.1 利用linkedList<Activity>管理应用中的界面 * 2.2 在ActivitylifecycleCallbacks的onActivityCreated() * 将Activity添加到linkedList中 * 在ActivitylifecycleCallbacks的onActivityDestroyed() * 将Activity从linkedList中移除 * 2.3 关闭应用时销毁linkedList中所有Activity * * 备注说明: * 1 ActivitylifecycleCallbacks是在API 14及其以上才有的 * 2 在AndroID4.0以下可以在BaseActivity中按照该思路实现 * */ public class MyApplication extends Application { private static int activityCounter=0; private static MyApplication mApplicationInstance; private static linkedList<Activity> mActivitylinkedList; private ActivitylifecycleCallbacksImpl mActivitylifecycleCallbacksImpl; @OverrIDe public voID onCreate() { super.onCreate(); mApplicationInstance=new MyApplication(); mActivitylinkedList=new linkedList<Activity>(); mActivitylifecycleCallbacksImpl=new ActivitylifecycleCallbacksImpl(); this.registeractivitylifecycleCallbacks(mActivitylifecycleCallbacksImpl); } public static MyApplication getInstance() { if (null==mApplicationInstance) { mApplicationInstance=new MyApplication(); } return mApplicationInstance; } //判断App是否在后台运行 public boolean isAppRunningBackground(){ boolean flag=false; if(activityCounter==0){ flag=true; } return flag; } //退出应用 public voID finishAllActivity(){ //unregisteractivitylifecycleCallbacks(mActivitylifecycleCallbacksImpl); System.out.println("--> mActivitylinkedList.size()="+mActivitylinkedList.size()); if(null!=mActivitylinkedList){ for(Activity activity:mActivitylinkedList){ if(null!=activity){ activity.finish(); } } } } private class ActivitylifecycleCallbacksImpl implements ActivitylifecycleCallbacks{ @OverrIDe public voID onActivityCreated(Activity activity,Bundle savedInstanceState) { System.out.println("onActivityCreated --> "+activity.getClass().getname()); if (null!=mActivitylinkedList&&null!=activity) { mActivitylinkedList.addFirst(activity); } } @OverrIDe public voID onActivityStarted(Activity activity) { activityCounter++; System.out.println("onActivityStarted --> "+activity.getClass().getname()+",activityCounter="+activityCounter); } @OverrIDe public voID onActivityResumed(Activity activity) { System.out.println("onActivityResumed --> "+activity.getClass().getname()); } @OverrIDe public voID onActivityPaused(Activity activity) { System.out.println("onActivityPaused --> "+activity.getClass().getname()); } @OverrIDe public voID onActivityStopped(Activity activity) { activityCounter--; System.out.println("onActivityStopped --> "+activity.getClass().getname()+",activityCounter="+activityCounter); } @OverrIDe public voID onActivitySaveInstanceState(Activity activity,Bundle outState) { System.out.println("onActivitySaveInstanceState --> "+activity.getClass().getname()); } @OverrIDe public voID onActivityDestroyed(Activity activity) { System.out.println("onActivityDestroyed --> "+activity.getClass().getname()); if (null!=mActivitylinkedList&&null!=activity) { if (mActivitylinkedList.contains(activity)) { mActivitylinkedList.remove(activity); } } } } }
FirstActivity如下:
package com.cc; import androID.app.Activity; import androID.content.Context; import androID.content.Intent; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; public class FirstActivity extends Activity { private Context mContext; private button mOpenNewActivitybutton; private button mFinishThisActivitybutton; private ClickListenerImpl mClickListenerImpl; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.first_activity); init(); } private voID init(){ mContext=this; mClickListenerImpl=new ClickListenerImpl(); mOpenNewActivitybutton=(button) findVIEwByID(R.ID.openNewActivitybutton); mOpenNewActivitybutton.setonClickListener(mClickListenerImpl); mFinishThisActivitybutton=(button) findVIEwByID(R.ID.finishThisActivitybutton); mFinishThisActivitybutton.setonClickListener(mClickListenerImpl); } private class ClickListenerImpl implements OnClickListener { @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.openNewActivitybutton: Intent intent=new Intent(mContext,SecondActivity.class); startActivity(intent); break; case R.ID.finishThisActivitybutton: finish(); break; default: break; } } } @OverrIDe protected voID onResume() { super.onResume(); //System.out.println(" first Activity onResume --> "+MyApplication.getInstance().isAppRunningBackground()); } @OverrIDe protected voID onStop() { super.onStop(); //System.out.println(" first Activity onStop --> "+MyApplication.getInstance().isAppRunningBackground()); } }
SecondActivity如下:
package com.cc; import androID.app.Activity; import androID.content.Context; import androID.content.Intent; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; public class SecondActivity extends Activity { private Context mContext; private button mOpenNewActivitybutton; private button mFinishThisActivitybutton; private ClickListenerImpl mClickListenerImpl; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.second_activity); init(); } private voID init(){ mContext=this; mClickListenerImpl=new ClickListenerImpl(); mOpenNewActivitybutton=(button) findVIEwByID(R.ID.openNewActivitybutton); mOpenNewActivitybutton.setonClickListener(mClickListenerImpl); mFinishThisActivitybutton=(button) findVIEwByID(R.ID.finishThisActivitybutton); mFinishThisActivitybutton.setonClickListener(mClickListenerImpl); } private class ClickListenerImpl implements OnClickListener { @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.openNewActivitybutton: Intent intent=new Intent(mContext,ThirdActivity.class); startActivity(intent); break; case R.ID.finishThisActivitybutton: finish(); break; default: break; } } } }
ThirdActivity如下:
package com.cc; import androID.app.Activity; import androID.content.Context; import androID.content.Intent; import androID.os.Bundle; import androID.vIEw.VIEw; import androID.vIEw.VIEw.OnClickListener; import androID.Widget.button; public class ThirdActivity extends Activity { private Context mContext; private button mFinishAllActivitybutton; private button mFinishThisActivitybutton; private ClickListenerImpl mClickListenerImpl; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.third_activity); init(); } private voID init(){ mContext=this; mClickListenerImpl=new ClickListenerImpl(); mFinishThisActivitybutton=(button) findVIEwByID(R.ID.finishThisActivitybutton); mFinishThisActivitybutton.setonClickListener(mClickListenerImpl); mFinishAllActivitybutton=(button) findVIEwByID(R.ID.finishAllActivitybutton); mFinishAllActivitybutton.setonClickListener(mClickListenerImpl); } private class ClickListenerImpl implements OnClickListener { @OverrIDe public voID onClick(VIEw v) { switch (v.getID()) { case R.ID.finishThisActivitybutton: finish(); break; case R.ID.finishAllActivitybutton: MyApplication.getInstance().finishAllActivity(); break; default: break; } } } @OverrIDe protected voID onResume() { super.onResume(); //System.out.println(" third Activity onResume --> "+MyApplication.getInstance().isAppRunningBackground()); } @OverrIDe protected voID onStop() { super.onStop(); //System.out.println(" third Activity onStop --> "+MyApplication.getInstance().isAppRunningBackground()); } @OverrIDe protected voID onDestroy() { super.onDestroy(); //System.out.println(" third Activity onDestroy --> "+MyApplication.getInstance().isAppRunningBackground()); } }
first_activity.xml如下:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.cc.FirstActivity" > <button androID:ID="@+ID/openNewActivitybutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerInParent="true" androID:text="open new Activity" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_above="@ID/openNewActivitybutton" androID:layout_centerInParent="true" androID:layout_marginBottom="50dip" androID:textSize="20sp" androID:textcolor="@androID:color/holo_red_light" androID:text="This is the first Activity" /> <button androID:ID="@+ID/finishThisActivitybutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@ID/openNewActivitybutton" androID:layout_centerHorizontal="true" androID:layout_margintop="50dip" androID:text="finish this activity" /> </relativeLayout>
second_activity.xml如下:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.cc.FirstActivity" > <button androID:ID="@+ID/openNewActivitybutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_centerInParent="true" androID:text="open new Activity" /> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_above="@ID/openNewActivitybutton" androID:layout_centerInParent="true" androID:layout_marginBottom="50dip" androID:textSize="20sp" androID:textcolor="@androID:color/holo_red_light" androID:text="This is the second Activity" /> <button androID:ID="@+ID/finishThisActivitybutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@ID/openNewActivitybutton" androID:layout_centerHorizontal="true" androID:layout_margintop="50dip" androID:text="finish this activity" /> </relativeLayout>
third_activity.xml如下:
<relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:paddingBottom="@dimen/activity_vertical_margin" androID:paddingleft="@dimen/activity_horizontal_margin" androID:paddingRight="@dimen/activity_horizontal_margin" androID:paddingtop="@dimen/activity_vertical_margin" tools:context="com.cc.FirstActivity" > <TextVIEw androID:ID="@+ID/textVIEw" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_above="@ID/openNewActivitybutton" androID:layout_centerHorizontal="true" androID:layout_margintop="100dp" androID:text="This is the third Activity" androID:textcolor="@androID:color/holo_red_light" androID:textSize="20sp" /> <button androID:ID="@+ID/finishThisActivitybutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@ID/textVIEw" androID:layout_centerHorizontal="true" androID:layout_margintop="50dip" androID:text="finish this activity" /> <button androID:ID="@+ID/finishAllActivitybutton" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@ID/finishThisActivitybutton" androID:layout_centerHorizontal="true" androID:layout_margintop="50dip" androID:text="finish All Activity" /> </relativeLayout>
AndroIDManifest.xml如下:
<?xml version="1.0" enCoding="utf-8"?> <manifest xmlns:androID="http://schemas.androID.com/apk/res/androID" package="com.cc" androID:versionCode="1" androID:versionname="1.0" > <uses-sdk androID:minSdkVersion="14" androID:targetSdkVersion="14" /> <application androID:name="com.cc.MyApplication" androID:allowBackup="true" androID:icon="@drawable/ic_launcher" androID:label="@string/app_name" androID:theme="@style/Apptheme" > <activity androID:name=".FirstActivity" androID:label="@string/app_name" > <intent-filter> <action androID:name="androID.intent.action.MAIN" /> <category androID:name="androID.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity androID:name="com.cc.SecondActivity"/> <activity androID:name="com.cc.ThirdActivity"/> </application> </manifest>
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
总结以上是内存溢出为你收集整理的Android 中 ActivityLifecycleCallbacks的实例详解全部内容,希望文章能够帮你解决Android 中 ActivityLifecycleCallbacks的实例详解所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)