Regaining focus to main activity after making a call from phonegap
android, cordova
Solution
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:
protected class RestartTask extends AsyncTask<Void, Void, Void>{
protected RestartTask() { }
@Override
protected Void doInBackground(Void... unused){
try {
// pass time so the built-in dialer app can make the call
Thread.sleep(MyPlugin.restartDelay);
}
catch (InterruptedException localInterruptedException)
{
Log.d("MyPlugin", "RestartTask received an InterruptedException");
}
return null;
}
@Override
protected void onPostExecute(Void justEyeCandy){
super.onPostExecute(justEyeCandy);
// Start the RestartActivity in a new task. This will snap the phone out of the built-in dialer app, which
// has started in it's own task at this point in time. The RestartActivity gains control and finishes
// immediately, leading control back to the activity at the top of the stack in the
// app (where the user came from when making the call).
Intent restartIntent = new Intent(MyPlugin.this.ctx.getApplicationContext(), RestartActivity.class);
restartIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
MyPlugin.this.ctx.getApplicationContext().startActivity(restartIntent);
}
}
This `AsyncTask` can be fired up in the plugin by calling:
this.ctx.runOnUiThread(new Runnable(){
public void run(){
try {
RestartTask restartTask = new RestartTask();
restartTask.execute();
}
catch (Exception e) {
Log.d("MyPlugin", "Exploded when trying to start background task: " + e.getMessage());
}
}
});
The class the AsyncTask starts in a new task is then:
public class RestartActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// the prototype RestartActivity there is a possibility that this will be the root activity of the app.
// If that is the case, this activity will boot the main activity of the app in a new task, before signing off.
// However, this is not the case for this app, as restarts are only used once a call diversion has taken place,
// form within the app.
if (isTaskRoot())
{
// Start the app before finishing
String packageName = this.getBaseContext().getPackageName();
Intent startAppIntent = this.getBaseContext().getPackageManager().getLaunchIntentForPackage(packageName);
startAppIntent.addFlags(Intent.FLAG_ACTIVITY_PREVIOUS_IS_TOP);
startActivity(startAppIntent);
}
// Now finish, which will drop the user in to the activity that was at the top of the task stack
finish();
}
}
Problem
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!