I have two activities, the first activity is launched by the Launcher, the second is started by the first. When I kill the process from within the first acitivity, the process gets killed. But when I kill it from within the second activity, the system would immediately launch a new process and the first activity. (The process's PID changed.) How can I do it cleanly?
I tried 3 different ways to kill the process: System.exit(0), android.os.Process.killProcess(pid), and non-programmatically from Eclipse's Devices panel.
Following are two world's most simple acitvities that I experiemented with. They both are outer classes in their respective files.
public class FirstActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
((Button)findViewById(R.id.button)).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(FirstActivity.this,
SecondActivity.class));
}});
}
}
public class SecondActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_2);
((Button)findViewById(R.id.button)).setOnClickListener(
new OnClickListener() {
public void onClick(View v) {
// Method 1
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
// Method 2
System.exit(0);
}});
}
}
(I know people say one should never provide a UI to exit the program. But this is for security reason. The user must have a way to exit the program and close the file so that his information won't be leaked.)