Reputation: 499
in the MainActivity , I have this code for closing the application :
@Override
public void onBackPressed() {
if (exit){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
super.onBackPressed();
}else {
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
I should press 2 back button when I want to close the application .
The problem is ,when I close the application, it starts from another activity , mostly from the last activity I've been to .
How can I always start the activity from mainActivity not other activities ?
Upvotes: 2
Views: 1389
Reputation: 2211
Write this part of code in every / in which activity is starting automatically,
@Override
public void onBackPressed() {
YourActivityName.this.finish();
}
Upvotes: 1
Reputation: 86
try the launchMode: singleTask or singleInstance
<activity
android:name=".MainActivity"
android:launchMode="singleInstance">
Upvotes: 1
Reputation: 8042
Use these lines of code..
@Override
public void onBackPressed() {
super.onBackPressed();
if (exit){
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_TASK_ON_HOME);
finish();
}else {
exit = true;
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
exit = false;
}
}, 3 * 1000);
}
}
Upvotes: 2
Reputation: 3237
override the onBackPressed method of your MainActivity as shown below
private int backpressedCount;
@Override
public void onBackPressed() {
backpressedCount++;
if (backpressedCount == 2) {
android.os.Process.killProcess(android.os.Process.myPid());
}
}
Upvotes: 1
Reputation: 2828
you have to kill all the activities when you are closing your application, have a look at this How to kill an application with all its activities?
Upvotes: 1