I am extending a thread class and from that class I want to start an activity. How to do this?
Asked
Active
Viewed 2.3k times
3 Answers
14
You need to call startActivity()
on the application's main thread. One way to do that is by doing the following:
Initialize a
Handler
and associate it with the application's main thread.Handler handler = new Handler(Looper.getMainLooper());
Wrap the code that will start the
Activity
inside an anonymousRunnable
class and pass it to theHandler#post(Runnable)
method.handler.post(new Runnable() { @Override public void run() { Intent intent = new Intent (MyActivity.this, NextActivity.class); startActivity(intent); } });

Alex Lockwood
- 83,063
- 39
- 206
- 250
-
1Makes no sense. You're using MyActivity.this. The person wants to start an activity from a thread. MyActivity.this is not a thread but referring to an activity. – Johann Aug 25 '13 at 04:58
-
4@AndroidDev Uh... what? This answer correctly describes how to start an activity from a background thread. `MyActivity.this` is just a reference to the current activity context (you could change it to `MyActivity.this.getApplicationContext()` if you wanted but it doesn't really matter). The main thing to understand here is that you can't call the `startActivity()` method from a background thread as it could cause weird race conditions. Instead, you need to post a runnable to the main thread's message queue to be executed on the main thread. Does that make sense? – Alex Lockwood Aug 25 '13 at 19:20
-
Hey guys, How should I start an activity on a thread that is already started. The above code shows to create a thread and then start it. Is it that I should start my thread in this way itself or is there any other way?? – Manpreet Nov 22 '14 at 08:28
-
@Manpreet Where does the code above "show how to create a thread and then start it"? The code above does not mention creating/starting threads at all, so I'm not sure what you mean. – Alex Lockwood Nov 23 '14 at 04:29
-
a new thread will be created when the run method will be called and in that we are starting a new activity. That is what I meant. Here is my case: http://stackoverflow.com/questions/27085511/android-starting-activity-from-a-separate-thread – Manpreet Nov 23 '14 at 04:32
-
1That is incorrect. A new thread does not get created when the run method is called. The runnable object is put in the main thread's message queue to be executed at a later time. When the message is executed, the main thread will simply call the runnable's `run()` method. There is no thread creation involved. The background thread simply tells the main thread to execute the runnable object whenever it gets the chance. Since the main thread executes the runnables sequentially one after another, we are guaranteed that no race conditions will occur. – Alex Lockwood Nov 23 '14 at 04:39
0
Well to start an activity of an class, a class should extends with activity according to me.
But if you want to start activity with some threading function you can do these things.
Instead of extends Thread, use implements Runnable. After that some class that have an Activity you just call the start thread and put your logic and that start Intent.
I think this is the good solution for you.

Alex Lockwood
- 83,063
- 39
- 206
- 250

Siten
- 4,515
- 9
- 39
- 64
0
you can use Something like this.
public class MyActivity extends Activity
{
Handler hander = new Handler(){
public void handleMessage(Message m){
Intent intent = new Intent (MyActivity.this, Next.class);
startActivity(intent);
}
};
pubilc void onCreate(Bundle ic)
{
//your code setContentView() etc....
Thread toRun = new Thread()
{
public void run()
{
hander.sendMessage(1);
}
}
toRun.start();
}
}

Adil Soomro
- 37,609
- 9
- 103
- 153
-
@iberck: thanks for pointing out. That was a post back from past when I just started the android dev. I now realized, that was severely and offensively buggy code. I've updated it. – Adil Soomro May 24 '13 at 16:30
-
This is still overly complicated... theres no need to override `handleMessage`. Just post a runnable to be executed on the main thread. The runnable will contain the code that will start the activity. – Alex Lockwood May 24 '13 at 16:35
-