I am extending a thread class and from that class I want to start an activity. How to do this?
You need to call startActivity() on the application's main thread. One way to do that is by doing the following:
startActivity()
Initialize a Handler and associate it with the application's main thread.
Handler
Handler handler = new Handler(Looper.getMainLooper());
Wrap the code that will start the Activity inside an anonymous Runnable class and pass it to the Handler#post(Runnable) method.
Activity
Runnable
Handler#post(Runnable)
handler.post(new Runnable() { @Override public void run() { Intent intent = new Intent (MyActivity.this, NextActivity.class); startActivity(intent); } });
2.1m questions
2.1m answers
60 comments
57.0k users