Synchronous startActivityForResult - waiting for activity to complete

I have an application in which I run a new action and there must be a result before continuing.

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

So, I think what I'm looking for is the best way to wait for activity to return ... Is something like this possible? Or is there a better way?

Startup launch 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 must be returned to a higher level call function - this is why I need to wait for the result in the ActivityLauncher function.

Thank!

+5
2

, . - , , onActivityResult

+7

, Android . Instrumentation. ? startActivitySync , . .

. , .

IntentFilter intF = new IntentFilter("ACTIVITY.THAT.YOU.WANT.TO.LAUNCH");

Instrumentation instrumentation = new Instrumentation();

Instrumentation.ActivityMonitor monitor = instrumentation.addMonitor(intF, null, true);
Intent i = new Intent("ACTIVITY.THAT.YOU.WANT.TO.LAUNCH");
instrumentation.startActivitySync(i);
+2

All Articles