How to kill a process?
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(Bu开发者_StackOverflowndle 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.)
You can try this.finish()
The other choices kill the process without informing the android system, which assumes something the user wanted to do has crashed and restarts it.
But your assumptions about closing the activity preventing data leakage is mistaken. If you want to provide a log out button, do that and have it negate whatever access tokens/keys were being used to obtain access.
Also think about what happens if some other activity comes to the foreground without the user exiting yours - for example, what if they receive a phone call?
Just my idea, After you start the second activity (this line),
startActivity(new Intent(FirstActivity.this,SecondActivity.class));
You can kill the first activity. Through,
int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);
so, now first activity didn't come.
Another way,
You can get first Activity's process id and send the process id to second activity, through bundle.
First kill the First Activity Process, next You can kill the second Activity process.
so, now first activity didn't come.
i had got that problem while i am newer in Android. i had created two activity and switch from one layout to anther layout but when i kill process of second activity, first activity will be resume(); and first layout will shown in display.
answer of your question is : in first activity resume you have to call finish(); method on in onResume() method.
System.exit(0); by use of this all current process will be kill
精彩评论