Reputation: 41
I'm making an android application in which there are different activities connected to Home (MainActivty) activity and vice-verse. I want to close my application whenever i press back key from home activity. But it takes me to previous activity where it is came from. how can i do this. Please someone help me in this, i'm new to android. Thanks.............
Here is my code:
btn_daily.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityA.class);
startActivity(i);
}
});
btn_health.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityB.class);
startActivity(i);
}
});
btn_diet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityC.class);
startActivity(i);
}
});
btn_exercise.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityD.class);
startActivity(i);
}
});
btn_yoga.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityE.class);
startActivity(i);
}
});
btn_help.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(getApplicationContext(), ActivityF.class);
startActivity(i);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
dialogOnBackPress();
return true;
}
return super.onKeyDown(keyCode, event);
}
protected void dialogOnBackPress() {
new AlertDialog.Builder(this)
.setTitle("Exit Alert !!")
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", null)
.show();
}
Upvotes: 0
Views: 65
Reputation: 1948
It seems that activities other then MainActivity
, has also some mechanism to navigate to other activities. So while navigating to other activity, don't forget to use finish()
to destroy that current activity.
And if your application has other mechanism like there is some home button which takes you to home screen from any navigation activity, then at that time open MainActivity
with FLAG_ACTIVITY_CLEAR_TOP
flag.
Hence by doing so, you will not have back stack of extra activity and back button from MainActivity
will close the app.
EDIT : For scenario you have mentioned in the comment, you have to use following code on Home button click.
btn_home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent i = new Intent(CurrentActivity.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
finish();
}
});
Upvotes: 1
Reputation: 186
Use this line in onBackPressed
method..
startActivity(new Intent(currentActivity.this,HomeActivity.class"));
finish();
Upvotes: 0
Reputation: 653
I think it is because you have not added intent flags
try this
new AlertDialog.Builder(this)
.setTitle("Exit Alert !!")
.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
}
})
Upvotes: 0