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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:49:26+00:00 2026-06-02T15:49:26+00:00

So activity starts and I create a Thread which checks when to go to

  • 0

So activity starts and I create a Thread which checks when to go to the next activity. But sometimes I need this activity to kill itself. onPause does this, but after that Thread is still alive and after time runs out it start a new activity. Is it possible to kill this Thread and stop goToFinals intent?

 public class Questions extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String in = getIntent().getStringExtra("time");         
        long tmp = Long.parseLong(in);
        endTime = (long) System.currentTimeMillis() + tmp;

        Thread progress = new Thread(new Runnable() {

            public void run() {
                while(endTime > System.currentTimeMillis()) {
                    try {
                        Thread.sleep(200);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
                Intent goToFinals = new Intent(Questions.this,End.class);
                        startActivity(goToFinals);
            }

        });
        progress.start();

    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}
  • 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-02T15:49:27+00:00Added an answer on June 2, 2026 at 3:49 pm

    There are a couple of ways you can stop your thread. If you store your Thread object then you can call interrupt() on it:

     progress.interrupt();
    

    This would cause the sleep() to throw an InterruptedException which you should return from, not just print the stack trace. You should also do the loop like:

     while(endTime > System.currentTimeMillis()
         && !Thread.currentThread().isInterrupted()) {
    

    You could also set some sort of shutdown flag:

    // it must be volatile if used in multiple threads
    private volatile boolean shutdown;
    
    // in your thread loop you do:
    while (!shutdown && endTime > System.currentTimeMillis()) {
        ...
    }
    
    // when you want the thread to stop:
    shutdown = true;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an Android activity which in turn starts a thread. In the thread
I need to create an activity that starts a service that monitors the users
I have an activity that starts after a certain time. Once this activity starts,
I have a series of activities in my application which starts with Activity A
I have 4 buttons that when click starts a new activity class. This works
I am want to pass data back from a Thread to Activity (which created
I have one thread class which star in onCreate of an activity. class MyThread
In the main thread I need to do the following: Create a second thread
I can create a class which extends Thread and overrides the run method with
I'm trying to create an intent to a new activity on an application but

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.