Reputation: 11419
My application should ask the user for a confirmation before putting the application in the background when the user presses the back button.
I tried to override dispatchKeyEvent
. The problem is that I also have fragments that are pushed in the backStack.
I should not ask the confirmation when there is still a fragment in the back stack because in that case the application won't go to the background: it will pop up the fragment from the stack.
Is there a way to distinguish between the case when the application will go to the background and when another fragment will be popped up from the stack in dispatchKeyEvent
?
If not is there another way to do it?
Thanks
Upvotes: 0
Views: 271
Reputation: 1399
You can override the onBackPressed method and get a list of current tasks from the activity manager and then decide weather to ask the user for conformation or just go back. This solutions is discussed here.
Upvotes: 1
Reputation: 4252
You can override
public void onBackPressed() {
super.onBackPressed();
}
and check for your condition like,
public void onBackPressed() {
if(foo == true)
showDialog();
else
super.onBackPressed();
}
Upvotes: 0
Reputation: 1698
use this method of Activity
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
// Add here whatever uoy want
}
Upvotes: 0
Reputation: 39397
just override onBackPressed.
Also, see http://developer.android.com/guide/components/tasks-and-back-stack.html for better understanding of the notion of backstack in android
Upvotes: 0