鱼C论坛

 找回密码
 立即注册
查看: 2643|回复: 0

[学习笔记] android手机编程2.6

[复制链接]
发表于 2017-9-22 06:19:20 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
把项目中所有的文件都改为继承字BaseActivity这个类,然后在主文件mainactivity.java旁边新建个java类BaseActivity
  1. package com.example.xinwei.lifecycle;

  2. import android.app.Activity;
  3. import android.os.Bundle;
  4. import android.support.annotation.Nullable;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.util.Log;

  7. /**
  8. * Created by xinwei on 2017/9/21.
  9. */

  10. public class BaseActivity extends AppCompatActivity {
  11.     @Override
  12.     protected void onCreate(@Nullable Bundle savedInstanceState) {
  13.         super.onCreate(savedInstanceState);
  14.         Log.i("BaseActivity",getClass().getSimpleName());
  15.         ActivityCollector.addActivity(this);
  16.     }

  17.     @Override
  18.     protected void onDestroy() {
  19.         super.onDestroy();
  20.         ActivityCollector.removeActivity(this);
  21.     }
  22. }
复制代码

然后再新建个ActivityCollector工具类文件
  1. package com.example.xinwei.lifecycle;

  2. import android.app.Activity;

  3. import java.util.ArrayList;
  4. import java.util.List;

  5. /**
  6. * Created by xinwei on 2017/9/22.
  7. */

  8. public class ActivityCollector {
  9.     public static List<Activity> activites = new ArrayList<>();
  10.     public static void addActivity(Activity activity){
  11.         activites.add(activity);
  12.     }
  13.     public static void removeActivity(Activity activity){
  14.         activites.remove(activity);
  15.     }
  16.     public static void finishAll(){
  17.         for(Activity activity:activites){
  18.             if(!activity.isFinishing()){
  19.                 activity.finish();
  20.             }
  21.         }
  22.     }
  23. }
复制代码

这样就可以很方便的释放空间了。然后将normal_layout.xml文件修改为:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"
  4.     xmlns:tools="http://schemas.android.com/tools"
  5.     android:layout_width="match_parent"
  6.     android:layout_height="match_parent"
  7.     tools:context="com.example.xinwei.lifecycle.NormalActivity">
  8. <Button
  9.     android:layout_width="match_parent"
  10.     android:layout_height="wrap_content"
  11.     android:text="finishAll"
  12.     android:onClick="click"/>
  13. </android.support.constraint.ConstraintLayout>
复制代码

NormalActivity.java修改为
  1. package com.example.xinwei.lifecycle;

  2. import android.support.v7.app.AppCompatActivity;
  3. import android.os.Bundle;
  4. import android.view.View;

  5. public class NormalActivity extends BaseActivity {

  6.     @Override
  7.     protected void onCreate(Bundle savedInstanceState) {
  8.         super.onCreate(savedInstanceState);
  9.         setContentView(R.layout.normal_layout);
  10.     }
  11.     public void click(View v){
  12.         ActivityCollector.finishAll();
  13.     }
  14. }
复制代码

进入NormalActivity这个活动后就可以直接退出整个程序了,效果图:
jdfw.gif

本帖被以下淘专辑推荐:

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-25 21:43

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表