Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6892265
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T06:30:48+00:00 2026-05-27T06:30:48+00:00

I have implemented Tab in my Application. Now I want to exit application with

  • 0

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]  
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T06:30:49+00:00Added an answer on May 27, 2026 at 6:30 am

    First you add an instance variable to your Activity that can tell you if its the first time the back button is pressed (a boolean that should be false as default – initialize it in onCreate):

    private boolean exiting;
    

    Then override onBackPressed():

    @Override
    public void onBackPressed() {
      if( !exiting )
        new ExitDialog( this ) ).show();
      else
        super.onBackPressed();
    }
    

    The ExitDialog should look as follows:

    private class ExitDialog extends AlertDialog implements OnClickListener {
      protected ExitDialog( Context context ) {
        super( context );
    
        setTitle( "Exit?" );
        setMessage( "Are you sure you want to exit?" );
        setButton( BUTTON_POSITIVE, "Yes", this );
        setButton( BUTTON_NEGATIVE, "No", this );
      }
    
      public void onClick( DialogInterface dialog, int which ) {
        switch( which ) {
          case BUTTON_POSITIVE:
            exiting = true;
            dialog.dismiss();
            MyActivity.this.onBackPressed();
            break;
          case BUTTON_NEGATIVE:
            exiting = false;
            dialog.dismiss();
            break;
        }
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have implemented application in which i am using tabs and want to fetch
I have a class which extends Application in an Android tabHost app. In the
I have an Android application that I've been working on, and it's implemented using
I want to use custom delegate in my search tab. I have never used
I have implemented following code in my application. -(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
I have implemented a ViewFlipper with 8 child view in my application as follows.
I have an Eclipse RCP Standalone Application using Eclipse 3.6. I have implemented a
I have a JavaFX 2.0 application with FXML. I want the components (TextFields, ComboBoxes,
I have to implement a tab bar based application. There are two requirements The
I have written a tab based application in Xcode/RestKit and am attempting to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.