I have implemented Tab in my Application. Now I want to exit application with showing “Are you sure” alert before app closes.
I tried this with
onKeyDown() and onBackPressed() methods but every time it gives Window BadToken Exception.
What could be problem and possible solution for this?
Please have look at stackTrace from LogCat
Thread [<1> main] (Suspended (exception WindowManager$BadTokenException))
ViewRoot.deliverKeyEventToViewHierarchy(KeyEvent, boolean) line: 2523
ViewRoot.handleFinishedEvent(int, boolean) line: 2443
ViewRoot.handleMessage(Message) line: 1735
ViewRoot(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 123
ActivityThread.main(String[]) line: 4633
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 521
ZygoteInit$MethodAndArgsCaller.run() line: 858
ZygoteInit.main(String[]) line: 616
NativeStart.main(String[]) line: not available [native method]
First you add an instance variable to your
Activitythat can tell you if its the first time the back button is pressed (a boolean that should befalseas default – initialize it inonCreate):Then override
onBackPressed():The
ExitDialogshould look as follows: