How to make sure activity show alert on exiting in onPause/onStop (Backpress is handled ) in current android version. I have a scenario where user need to give some assessment i need to make sure that a alert is shown to user before he leaves the assessment. I have seen few example on internet mostly people talking about backpressed/KEYGUARD but i need to show a dialog if user is mistakely clicked the home button or trying to exit the activity.
Below code didnt help
new OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if(keyCode == KeyEvent.KEYCODE_HOME){
//do something
}
return false;
}
});
You can Save the data, on Home Keypress. To show a dialog, you will have to do it out of the application context, because the home key will take the user to the main screen. When the user triggers the home button, the current activity closes. The user is taken to the main screen. At this stage, your activity is in the background. Your activity cannot trigger a dialog because the control is given to the Android OS.
According to the Android Docs, The Home key is handled by the framework and is never delivered to applications.
Essentially, you cannot force the user to stay in your Activity, after they have pressed the home button. As soon as the user triggers it, the control is given to Android. Speaking in context of your application, there is no way to override that.