欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android 中 ActivityLifecycleCallbacks的实例详解

程序员文章站 2023-12-18 22:34:52
android 中 activitylifecyclecallbacks的实例详解        &...

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> 

如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: