Activityが実行されているかどうかを判断するにはどうすればいいですか?
3649 ワード
方法1.
Googleエンジニアがどのように判断しているかを見てみましょう(AndroidソースのCallアプリケーション、AsyncTaskのonPostExecuteクリップから).@Override
protected void onPostExecute(Void result) {
final Activity activity = progressDialog.getOwnerActivity();
if (activity == null || activity.isDestroyed() || activity.isFinishing()) {
return;
}
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
isDestroyed()の判断が一つ増えた
方法2.Activity Collectorの定義 import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
* Author: jack
* Description: Activity
*/
public class ActivityCollector {
/**
* activity
*/
public static HashMap, Activity> activities = new LinkedHashMap<>();
/**
* Activity
*
* @param activity
*/
public static void addActivity(Activity activity, Class> clz) {
activities.put(clz, activity);
}
/**
* Activity
*
* @param clz
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isActivityExist(Class clz) {
boolean res;
Activity activity = getActivity(clz);
if (activity == null) {
res = false;
} else {
if (activity.isFinishing() || activity.isDestroyed()) {
res = false;
} else {
res = true;
}
}
return res;
}
/**
* activity
*
* @param clazz Activity
* @return
*/
public static T getActivity(Class clazz) {
return (T) activities.get(clazz);
}
/**
* activity, finish
*
* @param activity
*/
public static void removeActivity(Activity activity) {
if (activities.containsValue(activity)) {
activities.remove(activity.getClass());
}
}
/**
* Activity
*/
public static void removeAllActivity() {
if (activities != null && activities.size() > 0) {
Set, Activity>> sets = activities.entrySet();
for (Entry, Activity> s : sets) {
if (!s.getValue().isFinishing()) {
s.getValue().finish();
}
}
}
activities.clear();
}
}
AppのBaseActivityでpublic class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this, getClass());
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
そしてすべてのActivityがBaseActvityを継承することでActivity Collectorを機能させることができます.
メリット
@Override
protected void onPostExecute(Void result) {
final Activity activity = progressDialog.getOwnerActivity();
if (activity == null || activity.isDestroyed() || activity.isFinishing()) {
return;
}
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
}
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map.Entry;
import java.util.Set;
/**
* Author: jack
* Description: Activity
*/
public class ActivityCollector {
/**
* activity
*/
public static HashMap, Activity> activities = new LinkedHashMap<>();
/**
* Activity
*
* @param activity
*/
public static void addActivity(Activity activity, Class> clz) {
activities.put(clz, activity);
}
/**
* Activity
*
* @param clz
* @return
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static boolean isActivityExist(Class clz) {
boolean res;
Activity activity = getActivity(clz);
if (activity == null) {
res = false;
} else {
if (activity.isFinishing() || activity.isDestroyed()) {
res = false;
} else {
res = true;
}
}
return res;
}
/**
* activity
*
* @param clazz Activity
* @return
*/
public static T getActivity(Class clazz) {
return (T) activities.get(clazz);
}
/**
* activity, finish
*
* @param activity
*/
public static void removeActivity(Activity activity) {
if (activities.containsValue(activity)) {
activities.remove(activity.getClass());
}
}
/**
* Activity
*/
public static void removeAllActivity() {
if (activities != null && activities.size() > 0) {
Set, Activity>> sets = activities.entrySet();
for (Entry, Activity> s : sets) {
if (!s.getValue().isFinishing()) {
s.getValue().finish();
}
}
}
activities.clear();
}
}
AppのBaseActivityで
public class BaseActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCollector.addActivity(this, getClass());
}
@Override
protected void onDestroy() {
super.onDestroy();
ActivityCollector.removeActivity(this);
}
}
そしてすべてのActivityがBaseActvityを継承することでActivity Collectorを機能させることができます.
メリット
MainActivity mainActivity = ActivityCollector.getActivity(MainActivity.class);
if (mainActivity!=null) mainActivity.main_radio.check(R.id.radio_button1);
ActivityCollector.isActivityExist(MainActivity.class);