In my app I have 5 activities excluding main activity Im using other 4 activites repeatedly in cycle manner.
in my each activity (except main) onPause() method I have written Activityname.this.finish();
when I'm ending cycle on 5 th activity and returning back to main activity...
but my problem is when I'm ending main activity.. instead of closing app, it goes to 3 rd activity.
I don't know where is a problem exactly. may be in 3rd activity im using db n not closing it explicitly. Is this a problem?
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
PlayerDetails.this.finish();
}
this onPause() method I'm using
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if (keyCode==KeyEvent.KEYCODE_BACK){
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
alert.setIcon(R.drawable.ic_launcher);
alert.setTitle(R.string.app_name);
alert.setMessage("Really Exit?");
alert.setPositiveButton("Yes", new DialogInterface.OnClickListener(){
@Override
public void onClick(final DialogInterface dialog, final int which) {
MainActivity.this.finish();
dialog.dismiss();
}
});
alert.setNegativeButton("No", new DialogInterface.OnClickListener(){
@Override
public void onClick(final DialogInterface dialog, final int which) {
dialog.dismiss();
}
});
try{
AlertDialog dialog = alert.create();
dialog.show();
}
catch(Exception e){
e.printStackTrace();
}
}
return true;
}
this is my mainActivity method to close the app
You can use Flags instead of calling finish()
every time in each Activity. When you start a new Activity just use FLAG_ACTIVITY_CLEAR_TASK
followed by FLAG_ACTIVITY_NEW_TASK
with the Intent
which will clear the stack buffer and start the activity as a new. something like ..
Intent intent = new Intent(current_context, destination_activity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
You can find more information about Flags in this conversation.