Synchronous startActivityForResult - Waiting for Activity to Complete

你。 提交于 2019-11-29 13:48:18

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

Vicky Singh

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);
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!