「Androidの学習」Androidでアプリケーションを完全に終了するサンプルプログラム
方法1
applicationを使ってactivityを保存
public class TestApplication extends Application {
private LinkedList<Activity> sAllActivitys = new LinkedList<Activity>();
public void addActivity(Activity activity){
sAllActivitys.add(activity);
}
public void removeActivity(Activity activity){
sAllActivitys.remove(activity);
}
public LinkedList<Activity> getAllActivitys(){
return sAllActivitys;
}
}
方法2
package com.first.activity;
import java.util.LinkedList;
import android.app.Activity;
import android.os.Bundle;
public class BaseActivity extends Activity {
@オーバーライド
protected void onCreate(Bundle bundle) {
super.onCreate(bundle);
((MyApplication)getApplication()).addActivity(this);
}
@オーバーライド
protected void onDestroy() {
super.onDestroy();
((MyApplication)getApplication()).removeActivity(this);
}
protected void finishAll() {
LinkedList<Activity> sAllActivitys = ((MyApplication)getApplication()).getAllActivitys();
for(Activity activity : sAllActivitys) {
activity.finish();
}
sAllActivitys.clear();
}
protected void exit() {
finishAll();
System.exit(0);
}
}
Activity->BaseActivity、TabActivity->BaseActivity