Im building an app based on PhoneGAP, where I need to make a phone call and then return to my app after say 5 seconds.
The part about making a call works alright. To make Android open with a call and not just the dialpad, the code that makes the call is placed in a com.phonegap.api.Plugin and looks like
private void callNumber(String phoneNumber){
Uri calling = Uri.parse("tel:" + phoneNumber);
Intent callIntent = new Intent(Intent.ACTION_CALL, calling);
this.ctx.startActivity(callIntent);
}
To restart the app, Im launching an AsyncTask just before making the call called RestartTask. Because this code lives in a plugin, I have to use the Activity.runOnUiThread to start the RestartTask, but nothing special besides that.
In the RestartTask, only the doInBackground method is implemented and all it does, is sleeping for 5 seconds and then running the following intent:
Intent restartIntent = new Intent(DialerPlugin.this.ctx.getBaseContext(), MainActivity.class);
restartIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
DialerPlugin.this.ctx.startActivity(restartIntent);
Here MainActivity is the derived main class from PhoneGAP, which extends DroidGap.
Setting the FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP is exactly what people are saying here and here should be set, if an activity is to be “reactivated”, meaning that the task wherein the activity is currently instantiated is used instead of creating a new task, and the activity is reused with it’s running state, instead of creating a new activity instance. The “old” activity is given a call to onNewIntent when the intent is delivered by the OS.
However, nothing happens when the phone call becomes active and it seems the intent is not delivered to MainActivity, until I hang up with one of the phones. Weird.
If I change the flags to include FLAG_ACTIVITY_CLEAR_TOP, either the app task or the main activity is restarted. However since this is PhoneGAP, both corresponds to restarting the app, which is not what I wanted. I can also make Android boot an entirely new instance of my app in another task, which is given focus.
However I cannot make Android return focus to my main activity. What am I doing wrong?
Thanks!
I managed to solve this one after many hours of hacking.
It seems, that once a new android task takes control, which is what happens when the built-in dialer is used to make a call, the background task started from the task which made the call also loses its privilege to start new activities, unless it is starting the activity in a new task.
This means, that is an AsyncTask is started from the activity before making the call, then sleeping a bit to make sure the dialer task/activity is open, it must start a new task to gain back focus. It is not an option to start the Cordova activity again, as that would effectively restart the entire app, so the solution is to make a thin restart task, that finished immediately.
In the Cordova plugin, the following inner class can be placed:
This
AsyncTaskcan be fired up in the plugin by calling:The class the AsyncTask starts in a new task is then: