您好,登錄后才能下訂單哦!
當有3個activity時,在第三個activity想退出程序,那么就要按back鍵3次才能退出,那么怎么直接退出呢。
1、創建一個專門的集合類來管理所有活動
package com.yuanlp.taskstack; import android.app.Activity; import java.util.ArrayList; import java.util.List; /** * Created by 原立鵬 on 2017/6/27. */ public class ActivityCollector { public static List<Activity> activityList=new ArrayList<Activity>(); public static void addActivity(Activity activity){ activityList.add(activity); } public static void removeActivity(Activity activity){ activityList.remove(activity); } public static void finshiAll(){ for (Activity activity: activityList){ if (!activity.isFinishing()){ //判斷當前activity是否被干掉了 activity.finish(); } } activityList.clear(); } }
2、創建一個java的類,BaseActivity,繼承AppCompatActivity。為什么不直接寫成activity呢,因為我們不需要講BaseActivity在Manifext.xml里注冊。
package com.yuanlp.taskstack; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; /** * Created by 原立鵬 on 2017/6/27. */ public class BaseActivity extends AppCompatActivity{ private static final String TAG = "BaseActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, getClass().getSimpleName()); //獲取當前活動的名稱 ActivityCollector.addActivity(this); //調用自己寫的activity的集合類,把當前activity加入到list中 } protected void onDestroy(){ super.onDestroy(); ActivityCollector.removeActivity(this); //每當當前activity銷毀時,去除當前activity } }
3、讓3個activity都繼承BaseActivity,這樣之前寫的三個activity也不會報錯。
package com.yuanlp.taskstack; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; public class MainActivity extends BaseActivity { private static final String TAG = "MainActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "Task id is: "+getTaskId()); //輸出當前返回棧的ID setContentView(R.layout.activity_main); } @Override protected void onStart() { super.onStart(); Log.d(TAG, "onStart: "); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume: "); } @Override protected void onPause() { super.onPause(); Log.d(TAG, "onPause: "); } @Override protected void onStop() { super.onStop(); Log.d(TAG, "onStop: "); } @Override protected void onRestart() { super.onRestart(); Log.d(TAG, "onRestart: "); } @Override protected void onDestroy() { super.onDestroy(); Log.d(TAG, "onDestroy: "); } public void open1(View view){ Intent intent=new Intent(this,MainActivity.class); startActivity(intent); } public void open2(View view){ Intent intent =new Intent(this,SecondActivity.class); startActivity(intent); } }
4、在第三個activity中加入一個按鈕,點擊后退出程序。
package com.yuanlp.taskstack; import android.os.Bundle; import android.util.Log; import android.view.View; public class ThirdActivity extends BaseActivity { private static final String TAG = "ThirdActivity"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "Task id is"+getTaskId()); setContentView(R.layout.activity_third); } public void exitApp(View view){ ActivityCollector.finshiAll(); //退出 android.os.Process.killProcess(android.os.Process.myPid()); //殺死當前進程 } }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。