android 退出APP

2022-09-09 15:24:46 浏览数 (1)

大家好,又见面了,我是你们的朋友全栈君。

在onCreate() 中将Activity 实例 放到 线性容器中,,,, 退出时,一顿 ((Activity)list.gert(i)).finsh();

存在的问题也是很明显的。。。

保存了Activity的引用,是否会涉及,内存回收的问题。。。。(你得直到下面用的 是 强引用 的方式 哦。)

代码语言:javascript复制
package com.mystore.customer.activity;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import java.lang.Thread.UncaughtExceptionHandler;
import java.util.LinkedList;
import java.util.List;

import mylog.Log;
import android.app.Activity;
import android.app.Application;
import android.content.pm.PackageManager.NameNotFoundException;

import com.mystore.customer.activity.lookfor.LookForCustomerActivity;
import com.mystore.customer.activity.more.MoreMessageActivity;
import com.mystore.customer.activity.ramble.HotStoreActivity;
import com.mystore.customer.activity.search.SearchMainActivity;

public class CustomerApplication extends Application {
	
	private static final String TAG = "CustomerApplication";
	
	private static Application application;
	
	public static String VERSION = "1.1";

	public static Long EXIT_TIMEOUT = 2500l;
	
	//add activity container,used to exit app 
	private List<Activity> activityList = new LinkedList<Activity>();
	
	private static CustomerApplication instance;
	
	// 单例模式中获取唯一的ExitApplication 实例
	public static CustomerApplication getInstance() {
		if (null == instance) {
			instance = new CustomerApplication();
		}
		return instance;

	}
	 
	// 添加Activity 到容器中
	public void addActivity(Activity activity) {
		 activityList.add(activity); 
	}
	  
	// 遍历所有Activity 并finish

	public void exit() {
		for (Activity activity : activityList) {
			activity.finish();
		}

		System.exit(0);
	}
	
	public void popToMainTab(){
		
		for (int i=activityList.size()-1;i>0;i--) {
			Activity activity = activityList.get(i);
			
			if(activity!=null){
				if(
						  (HotStoreActivity.class).equals(activity.getClass())
						  || (MainActivity.class).equals(activity.getClass())
						  || (SearchMainActivity.class).equals(activity.getClass())
						  || (MoreMessageActivity.class).equals(activity.getClass())
						  || (LookForCustomerActivity.class).equals(activity.getClass())
						
				){
					//跳出循环
					break;
				}else{
					activity.finish();
				}
			}
		}
	}
	 

	@Override
	public void onCreate() {
		super.onCreate();

		Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler() {
			public void uncaughtException(Thread thread, Throwable throwable) {
				final Writer result = new StringWriter();
				final PrintWriter printWriter = new PrintWriter(result);
				throwable.printStackTrace(printWriter);
				String strStacktrace = result.toString();
				printWriter.close();

				Log.e("", "================================");
				Log.e("", "Force close occur, thread is: "   thread
						  " throwable: "   throwable);
				Log.e("", " stacktrace "   strStacktrace);
				Log.e("", "================================");
			}
		});
		
		application = this;
		try {
			VERSION = this.getPackageManager().getPackageInfo(this.getPackageName(), 0).versionName;
		} catch (NameNotFoundException e) {
			e.printStackTrace();
		}
	}
	
	public static Application getApplication() {
		return application;
	}

}



 发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/160724.html原文链接:https://javaforall.cn
                     

0 人点赞