Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
860 views
in Technique[技术] by (71.8m points)

android - Synchronous startActivityForResult - Waiting for Activity to Complete

I have an application where I am launching a new Activity, and need to have the result of the activity before proceeding.

I realize that startActivityForResult is asynchronous / non-blocking, and that I can get the result of the activity in the onActivityResult callback.

So I guess what I'm looking for is the best way to wait for the activity to return... Something like this perhaps? Or is there a better way?

Activity Launcher Function:

public String ActivityLauncher()
{
   //Set up Intent
   startActivityForResult(intent, 1);
   while (mIsActivityDone == false)
   {
       Thread.Sleep(250);
   }
   //Continue with processing
   String data = "<Data from Activity">
   return data;
}

Callback:

protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
   //Pull out the data
   mIsActivityDone = true;
}

The data needs to be returned to a higher level calling function - this is why I need to wait for the result in the ActivityLauncher function.

Thanks!

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This will block your UI thread so other activity will not execute either. I would use some kind of callback class provided by higher level function, in this kind of situation and call its callback function in onActivityResult


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...