My strategy is to only show a Toast (and maybe Vibration) (warning the
user that the next Back will exit the app) if the user has already
very recently (<2 secs ago) hit Back to return to the main activity
from a secondary activity.

This doesn't help the situation of accidentally hitting the back key
when you are trying to hit something else.

But in my app, its more common that users do something like Back-Back-
Back in an attempt to go to the main activity but actually overshoot
and exit the app. This solves that problem.

On Apr 28, 9:11 am, "Michael A." <[email protected]> wrote:
> Hi Xiongzh,
>
> Do something like the following where appropriate:
>
> @Override
> public boolean onKeyDown(int keyCode, KeyEvent event) {
>   if (keyCode == KeyEvent.KEYCODE_BACK) {
>     AlertDialog.Builder builder = new AlertDialog.Builder(this);
>       builder.setMessage("Quit Y/N?")
>                .setCancelable(false)
>                .setPositiveButton("Yes", new
> DialogInterface.OnClickListener() {
>                    public void onClick(DialogInterface dialog, int id)
> {
>                        MyActivity.this.finish();
>                    }
>                 })
>                .setNegativeButton("No", new
> DialogInterface.OnClickListener() {
>                    public void onClick(DialogInterface dialog, int id)
> {
>                        dialog.cancel();
>                    }
>             });
>         builder.show();
>         return true;
>     }
>     return super.onKeyDown(keyCode, event);
>
> }
>
> There are probably a few bugs in the above, but hopefully you get the
> basic idea.
>
> Regards,
>
> Michael A.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group 
> athttp://groups.google.com/group/android-developers?hl=en

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to