开发者

What is the mechanism behind startActivityForResult() in Android?

开发者 https://www.devze.com 2023-01-11 11:33 出处:网络
I have an activity. In this activity I want to start another activity using startActivityForResult(). I understand that my basic activity is started within a process with a main GUI thread.

I have an activity. In this activity I want to start another activity using startActivityForResult(). I understand that my basic activity is started within a process with a main GUI thread. But 开发者_JAVA百科as far as I understand, startActivityForResult() is asynchronious which means that my new activity will be executed in a different thread. I can't find information regarding the threads inside. If there is only one GUI thread, how does these functions work asynchroniously ?


But as far as I understand, startActivityForResult() is asynchronious which means that my new activity will be executed in a different thread.

startActivityForResult() is asynchronous. That does not mean that your new activity will be executed in a different thread. If the new activity is part of your own application, it runs on the main application thread, like all your other activities.

If there is only one GUI thread, how does these functions work asynchroniously ?

startActivityForResult(), like startActivity(), does not do anything immediately. Rather, it puts a message on a message queue, then returns. When you return control back to Android (e.g., your onClick() method ends), Android goes back to processing messages off of that queue. When it gets to your start-activity message, it starts up the new activity.


pass any info you want in the form of Extras in your Intent.

Intent i = new Intent(getApplicationContext(), YourClass.class);
i.putExtra("EXTRA_INFO", <your info here>);
startActivityForResult(i);

And in your new activity

protected void onCreate(Bundle savedInstanceState) {
      if(getIntent().hasExtra("EXTRA_INFO"){
         mString =  getIntent().getStringExtra("EXTRA_INFO");
      } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号