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 8337951
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T04:26:40+00:00 2026-06-09T04:26:40+00:00

I have 2 buttons when i click back button i am delaying exit by

  • 0
  1. I have 2 buttons
  2. when i click back button i am delaying exit by 10 seconds
  3. if any button among 2 buttons is pressed then i want to stop exit of app.
  4. if no button pressed detected then continue exit.

public class MainActivity extends Activity {

int count=0;
boolean pressed;
Button b1,b2;
TextView t1;
Thread t;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    b1= (Button) findViewById(R.id.button1);
    b2= (Button) findViewById(R.id.button2);
    t1 = (TextView) findViewById(R.id.textView2);

    b1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            pressed=true;
            t1.setText("Button1 Pressed");
            System.out.println("Button1");


        }
    });
    b2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {

            pressed=true;
            t1.setText("Button2 Pressed");
            System.out.println("Button2");


        }
    });
}
/* (non-Javadoc)
 * @see android.app.Activity#onBackPressed()
 */
@SuppressLint("ParserError")
@Override
public void onBackPressed() {
    System.out.println("Inside Back Button");
    pressed=false;
    t = new Thread(new ABC(), "My Thread");

    t.start();
    System.out.println("OutSide while Loop in BACKBUTTON");
    if(pressed==true){
        System.out.println("Button 1 or 2 Pressed");
        t.interrupt();
        System.out.println("Stopping Thread forcefully");

        count =0;
    }
    if(pressed==false){
        System.out.println("Button 1 or 2 NOT Pressed");
        t.stop();
        super.onBackPressed();
    }
}


public class ABC implements Runnable{



    @Override
    public void run() {
        System.out.println("Inside Thread");
        do{
            count++;
            System.out.println(count);

        }
        while((count <1000) && (pressed==false));
        System.out.println("OutSide while Loop");

    }

}

}

  • 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-06-09T04:26:42+00:00Added an answer on June 9, 2026 at 4:26 am

    Using Handler is your best solution here. When back pressed add runnable that calls Activity.finish() via handler.postDelayed(yourRunnableThatCallsFinish, 10000). Be sure to call handler.removeCallbacks(SameRunnableObjectThatSendToQueueViaPostDelayed) at button’s onClick events to prevent execution of that runnable.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have open pdf file through my application.when click on device back button it
I have multiple buttons on a page and I want to simulate a click
How to handle a back button in an activity? I have some buttons. If
I have 4 buttons that when click starts a new activity class. This works
I have two buttons, which the user can click and will open a FileDiagloag
I have these buttons inside a div, next to each other. When I click
I have three radio buttons with same name and different values.When I click the
I have a few buttons on my JS page. On click of those, I
In my application I have some link buttons there but when I right click
I have dynamically created the buttons in a div. And binding the click and

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.