Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 763
J J Avatar asked Jun 14 '11 23:06

J J


People also ask

What is the use of startactivityforresult in Android?

The android startActivityForResult method, requires a result from the second activity (activity to be invoked). In such case, we need to override the onActivityResult method that is invoked automatically when second activity returns result. Method Signature. There are two variants of startActivityForResult() method.

When should the onactivityresult method be invoked?

The onActivityResult method thus needs to be overridden when the second activity returns the result, i.e, it needs to be invoked automatically when the second activity returns the result. public void startActivityForResult (Intent intent, int requestCode)

What is the difference between onactivityresult and return a result?

A result is needed by the android startActivityForResult method from the second activity, i.e, the activity to be invoked. The onActivityResult method thus needs to be overridden when the second activity returns the result, i.e, it needs to be invoked automatically when the second activity returns the result.

How to get the result from another activity in Android?

The result from another activity can be received by using the android startActivityForResult () method, i.e., the information from one activity can also be sent to another activity and is the same for vice-versa, by using the android startActivityForResult () method.


2 Answers

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

like image 182
Alex Gitelman Avatar answered Sep 21 '22 08:09

Alex Gitelman


If you can start these activities from a new thread then Android has a solution for you. You can use Instrumentation. Why do you need a new thread? Because startActivitySync will check if you its being invoked in a main thread. If its main thread then it will throw an exception.

Instrumentation lets you monitor an activity. You can monitor this activity in either blocking or non blocking fashion.

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);
like image 32
Vicky Singh Avatar answered Sep 21 '22 08:09

Vicky Singh