新网创想网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
Android 中 ActivityLifecycleCallbacks如何使用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
专注于为中小企业提供成都做网站、网站制作服务,电脑端+手机端+微信端的三站合一,更高效的管理,为中小企业沙河免费做网站提供优质的服务。我们立足成都,凝聚了一批互联网行业人才,有力地推动了1000+企业的稳健成长,帮助中小企业通过网站建设实现规模扩充和转变。
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管理应用中的界面 * 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 mActivityLinkedList; private ActivityLifecycleCallbacksImpl mActivityLifecycleCallbacksImpl; @Override public void onCreate() { super.onCreate(); mApplicationInstance=new MyApplication(); mActivityLinkedList=new LinkedList (); 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如下:
second_activity.xml如下:
third_activity.xml如下:
AndroidManifest.xml如下:
关于Android 中 ActivityLifecycleCallbacks如何使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联行业资讯频道了解更多相关知识。