Note that there are some explanatory texts on larger screens.

plurals
  1. POneed help in Home Button in android
    text
    copied!<p>hi all i want to intercept my Home Button.<br> what i want is that whenever i press a Home Button i want to display a Alert dialog for Are you sure you want to Quit. if Yes then Finish the activity else do nothing. I have got to know that<br> when ever we Press Home Button the following callbacks are performed in order.</p> <p>onSaveInstanceState(Bundle outState) </p> <p>onPause() </p> <p>onStop() </p> <p>So i have overRide onSaveInstanceState Method and set my Alert dialog Code there but it gives me Exception on Dialog. Please Help friends. Guide me a lil..about it.</p> <p><strong>UPDATED:</strong> </p> <pre><code>@Override public void onSaveInstanceState(Bundle savedInstanceState) { boolean flag = displayAlertDialog(); if(flag){ this.finish(); super.onSaveInstanceState(savedInstanceState); } } </code></pre> <p><strong>displayAlertDialog Method:</strong> </p> <pre><code>private boolean isExit = false; public boolean displayAlertDialog() { //final boolean flag=true; int a = 0; AlertDialog.Builder alt_bld = new AlertDialog.Builder(this); alt_bld.setMessage("Are you sure you want to Exit?") .setPositiveButton("Yes", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub isExit = true; } }) .setNegativeButton("No", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { // TODO Auto-generated method stub isExit = false; dialog.cancel(); } }); alt_bld.show(); return isExit; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload