Partly excerpted from "The First Line of Code" Guo Lin
1. Find the activity used by the current interface
Create a new class to inherit Activity and override its onCreate() method
public class BaseActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); Log.e("BaseActivity" , getClass().getSimpleName()); } }· Make BaseActivity the parent class of all activities in the project and run the project. It can be done under Android Monitor -> Error
See the activities used in the current interface.
2. Exit the program anytime, anywhere
Create a new activity manager class
public class ActivityCollector { public static List<Activity> activities = new ArrayList<Activity>(); public static void addActivity(Activity activity ){ activities.add(activity); } public static void removeActivity(Activity activity ){ activities.remove(activity); } public static void finishAll(){ for(Activity activity : activities){ if(!activity.isFinishing()){ activity.finish(); } } }
Define the parent class that each activity needs to inherit
public class faActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy (); ActivityCollector.removeActivity(this); } }
After that, no matter where you are, just call ActivityCollector.finishAll(); method to exit the program
Note:
finish(): Ends the current Activity without immediately releasing the memory. Follow the android memory management mechanism.
System.exit();和android.os.Process.killProcess(android.osProcess.myPid());
End the current component such as Activity, and immediately release the resources occupied by the current Activity.
Online test of several exit methods:
public class BaseActivity extends Activity implements View.OnClickListener{ public static void actionStart(Context context , String data1 , String data2){ Intent intent = new Intent(context ,BaseActivity.class); intent.putExtra("name1" , data1 ); intent.putExtra("name2" , data2); context.startActivity(intent); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.baseactivity); Button one = (Button) findViewById(R.id.one); Button two = (Button) findViewById(R.id.two); Button three = (Button) findViewById(R.id.three); Button four = (Button) findViewById(R.id.four); one.setOnClickListener(this); two.setOnClickListener(this); three.setOnClickListener(this); four.setOnClickListener(this); ActivityCollector.addActivity(this); } @Override protected void onDestroy() { super.onDestroy (); ActivityCollector.removeActivity(this); } @Override public void onClick(View view) { switch (view.getId()){ case R.id.one: android.os.Process.killProcess(android.os.Process.myPid()); // killProcess break; case R.id.two: System.exit(0); break; case R.id.three: ActivityManager am = (ActivityManager)getSystemService (Context.ACTIVITY_SERVICE); am.restartPackage(getPackageName()); break; case R.id.four: ActivityCollector.finishAll(); // android.os.Process.killProcess(android.os.Process.myPid()); break; default: break; } } }
public class MainActivity extends BaseActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate (savedInstanceState); setContentView(R.layout.activity_main); Button mbutton = (Button) findViewById(R.id.mbutton); mbutton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { BaseActivity.actionStart(MainActivity.this , "data1" , "data2"); } }); } }
Result: 1,2 will kill the current Activity and rebuild the MainActivity interface. 3 is invalid, 4 exits successfully.