Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7899849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T08:48:54+00:00 2026-06-03T08:48:54+00:00

Im building an app based on PhoneGAP, where I need to make a phone

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T08:48:56+00:00Added an answer on June 3, 2026 at 8:48 am

    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();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am building a tile based app in Python using pyglet/openGL wherein I'll need
I'm building a calendar-based web app, and am in great need of a javascript
I am building an app where I need to change some helpful information based
I'm building a small web based management app. Within it I need to connect
I am building a business app based on a tiny variation of the Prism
I'm currently building a new app based on a legacy database. In the app,
I'm building a Sinatra based app for deployment on Heroku. You can imagine it
I am building an app using listview and lazy adapter based on @fedor post
I am building a web app using Codeigniter 2.0.3. I need to create base
We are building a PhoneGap app (both for iOS and Android) that has a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.