Android 中 ActivityLifecycleCallbacks如何使用
短信预约 -IT技能 免费直播动态提醒
Android 中 ActivityLifecycleCallbacks如何使用,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。
MyApplication如下:
package com.cc; import java.util.LinkedList; import android.app.Activity; import android.app.Application; import android.os.Bundle; 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如何使用问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注编程网行业资讯频道了解更多相关知识。
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341