How to do a blocking and synchronized activity?
Well, please don't ask me why, but I need to start a synchronous and blocking activity so that the program flow won't continue until it's finished. I know how to do a synchronous Dialog, but how can I do a synchronous activity?
Following are two approaches I tried but failed:
// In the 1st activity, start the 2nd activity in the usual way
startActivity(intent);
Looper.loop(); // but pause the program here
// Program continuse running afer Looper.loop() returns
....
// Then, in the second activity's onBackPressed method:
public void onBackPressed() {
// I was hoping the following quit() will terminate the loop() call in
// the first activity. But it caused an exception "Main thread not allowed
// to quit." Understandable.
new Hanlder().getLooper().quit();
}
I also tried to use another thread to achieve this:
// In the first activity, start a thread
SecondThread m2ndThread = new SecondThread(this);
Thread th = new Thread(m2ndThread, "Second Thread");
th.run();
synchronized(m2ndThread) {
m2ndThread.wait();
}
class SecondThread implements Runnable {
Activity m1stActivity;
SecondThread(Activity a) {
开发者_JAVA技巧 m1stActivity = a;
}
public void run() {
Looper.prepare();
Handler h = new Handler() {
public void handleMessage() {
Intent intent = new Intent(m1stActivity, SecondActivity.class);
m1stActivity.startActivity(intent); // !!!!!!!!!!!!!!
}
}
h.sendEmptyMessage(10); // let it run
Looper.quit();
}
}
However, this approach doesn't work because when I'm using the 1st activity to start the 2nd activity, the main thread is already in wait state and doing nothing. So the 2nd activity didn't even get created. (This is ironical: You have to use an activity to start an activity, but how can you do that when it's already in wait state?)
You can't.
That is all there is to it. You just can't do this. Activities execute on the main thread, always. Your main thread must be actively running its event loop. It is ALWAYS broken to block the main thread's event loop. Always.
All activities are asynchronous. But if you want to block the rest of your application flow until the activity finishes, you can inject a dependency on a flag that the activity sets. The most encapsulated way to do so would be to carry the flag along with the intent/activity/return flow. You could:
Declare the flag in that object's global scope:
boolean syncBlock = false;
int ASYNC_ACTIVITY_REQUEST_CODE = 1; // Arbitrary unique int
In the object's scope that starts the new activity include:
Intent asyncIntent = new Intent(this, AsyncActivity.class);
syncBlock = true;
startActivityForResult(asyncIntent, ASYNC_ACTIVITY_REQUEST_CODE);
while(syncBlock) {}
And in in the object that started the activity:
onActivityResult(int requestCode, int resultCode, Intent data)
{
switch(requestCode)
{
case ASYNC_ACTIVITY_REQUEST_CODE:
{
syncBlock = false;
break;
}
[...]
}
It's a crude hack, and if you're blocking in your UI thread (eg. in your MAIN activity) you're blocking everything, including other UI features your users will probably expect to respond but won't. It's a big nono, and you should learn to go with the async flow that makes apps work the Android way. But if you absolutely must...
精彩评论