Synchronous startActivityForResult - Waiting for Activity to Complete

后端 未结 2 1303
一个人的身影
一个人的身影 2020-12-20 16:14

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 as

相关标签:
2条回答
  • 2020-12-20 16:52

    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);
    
    0 讨论(0)
  • 2020-12-20 17:11

    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

    0 讨论(0)
提交回复
热议问题