1. 程式人生 > >Android中退出多個Activity的兩個經典方法

Android中退出多個Activity的兩個經典方法

一種把每個activity用集合記住,然後逐一干掉;另一種思路是使用廣播。

方法一、用list儲存activity例項,然後逐一干掉

建立一個外部類繼承Application用來存放activity

public class MyActvity extends Application {
    //建立一個集合,用來存放activity的物件
    ArrayList<Activity>list=new ArrayList<>();
    //宣告一個本類的物件
    private static MyActvity instance;
    public
MyActvity() { } //建立一個方法,用來初始化MyActivity的物件,並且初始化的對像的返回 public synchronized static MyActvity getInstance(){ if (instance==null){ instance=new MyActvity(); } return instance; } //呼叫此方法用來向集合當中新增activity物件 public void addActivity(Activity activity){ list.add(activity); } //判斷activity是否已經在集合當中
public boolean isexitlist(Activity activity){ if (list.contains(activity)){ return true; } return false; } //當呼叫此方法的時候,關閉所有的activity public void exit(){ for (Activity activity:list){ activity.finish(); } //退出當前的MyActivity
System.exit(0); } @Override public void onLowMemory() { super.onLowMemory(); //當系統的儲存空間不夠的時候,呼叫系統的垃圾回收期,清理裡面的垃圾 System.gc(); } }

Activity1程式碼:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //呼叫存放activity類
        MyActvity instance = MyActvity.getInstance();
        //判斷存放activity類是否存放該activity,不存在加入類
        if (!instance.isexitlist(this)){
            instance.addActivity(this);
        }
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }
}

Activity2程式碼:

public class Main2Activity extends Activity {

    private MyActvity instance;

    /**
     * Called when the activity is first created.
     */

    @Override+
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sdfa);
        Button bt= (Button) findViewById(R.id.bt);
        instance = MyActvity.getInstance();
        if (!instance.isexitlist(this)){
            instance.addActivity(this);
        }
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            //呼叫exit()方法銷燬裡面所有activity
                instance.exit();
            }
        });

    }
}

方法二、使用廣播在activity裡註冊廣播,銷燬時啟動廣播

MainActivity裡註冊廣播:


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
        //註冊廣播
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Define.PAHNAME);
        registerReceiver(new MyReceiver(),intentFilter);
    }
    class MyReceiver extends BroadcastReceiver{

        @Override
        public void onReceive(Context context, Intent intent) {
            unregisterReceiver(this);
            ((Activity)context).finish();
        }
    }
}

activity2:啟動廣播

public class Main2Activity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sdfa);
        Button bt= (Button) findViewById(R.id.bt);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(Define.PAHNAME);
                sendBroadcast(intent);
                finish();
            }
        });

    }
}