Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am extending a thread class and from that class I want to start an activity. How to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
804 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to call startActivity() on the application's main thread. One way to do that is by doing the following:

  1. Initialize a Handler and associate it with the application's main thread.

    Handler handler = new Handler(Looper.getMainLooper());
    
  2. Wrap the code that will start the Activity inside an anonymous Runnable class and pass it to the Handler#post(Runnable) method.

    handler.post(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent (MyActivity.this, NextActivity.class);
            startActivity(intent);
        }
    });
    

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...