Question

I make a phone call from an Activity and when call ends I want to come back to application. I tried all the solutions available on stackoverflow. One of them used to work for few minutes but not working now.

I tried using recreate() method which successfully calls onCreate method of an Activity but app is not in foreground. I tried using various flags such as FLAG_ACTIVITY_CLEAR_TOP, FLAG_ACTIVITY_CLEAR_TASK, FLAG_ACTIVITY_NO_HISTORY. But does not work.

Code to go back to application from call app :

private class PhoneCallListener extends PhoneStateListener {

    private boolean isPhoneCalling = false;

    @Override
    public void onCallStateChanged(int state, String incomingNumber) {

        // If call ringing
        if (state == TelephonyManager.CALL_STATE_RINGING) {

        }
        // Else if call active
        else if (state == TelephonyManager.CALL_STATE_OFFHOOK) {

            isPhoneCalling = true;
        }
        // Else if call idle
        else if (state == TelephonyManager.CALL_STATE_IDLE) {

            if (isPhoneCalling) {

                isPhoneCalling = false;

                MyActivity.this.recreate();
            }
        }
    }
}
Was it helpful?

Solution 4

The solution is to launch an application with an extra included in bundle that says that app is launched as part of coming back from any other app.

When call ends, I launch an app using below code :

// Launch app
Intent i = new Intent(ActivityThatMadeCall.this,
LauncherActivity.class);
i.putExtra("EXTRA_RETURNED_FROM_CALL_APP", true);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);


Then in my launcher activity I check for the extra and if it exists then start the activty that placed call. :

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    if(getIntent().getExtras().getString("EXTRA_RETURNED_FROM_CALL_APP") != null) {
        startActivity(new Intent(this, ActivityThatMadeCall.class));
    }
}

OTHER TIPS

This is my solution, it works perfectly on 4.3 - other OS versions not tested yet, but everything should be fine.

Registering the listener in MainActivity:

TelephonyManager tManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
    listener = new ListenToPhoneState();
    tManager.listen(listener, PhoneStateListener.LISTEN_CALL_STATE);

The PhoneStateListener from MainActivty:

private class ListenToPhoneState extends PhoneStateListener {

    private String LOG_TAG = "mainactivity";
    private boolean isCallFinished = false;

    public void onCallStateChanged(int state, String incomingNumber) {
        if (TelephonyManager.CALL_STATE_RINGING == state) {
            Log.i(LOG_TAG, "RINGING, number: " + incomingNumber);
        }
        if (TelephonyManager.CALL_STATE_OFFHOOK == state) {
            // wait for phone to go offhook (probably set a boolean flag) so
            // you know your app initiated the call.
            isCallFinished = true;
            Log.i(LOG_TAG, "OFFHOOK");
        }
        if (TelephonyManager.CALL_STATE_IDLE == state) {
            // when this state occurs, and your flag is set, restart your
            // app
            if (isCallFinished) {
                isCallFinished = false;
                Intent i = new Intent(getApplicationContext(),
                        MainActivity.class);
                // this needs if you want some special action after the phone call 
                //ends, that is different from your normal lauch configuration
                i.setAction("SHOW_PHONE_CALL_LIST");
                startActivity(i);
                finish();
            }
            Log.i(LOG_TAG, "IDLE");
        }
    }

}

I start the phone call from a fragment, but that doesn't make any difference:

Uri uri = Uri.parse("tel:" + Uri.encode(callIt));
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
getActivity().finish();

You have to call finish() on the activity that starts the call, otherwise after the phone call, your app will stay behing the default Phone application.

When your app is starting, you can have a look at the intent, and you can set up your after call configuration. Call this in your onCreate() method:

if (intent.getAction().equals("SHOW_PHONE_CALL_LIST")) {
        //perfom after call config here
    }

I hope everything is clearly explained.

I think you must use a broadcast receiver and start the activity upon uphook. Also you need to declare permissions to read phone state.

  1. Try to modify your code, that the important stuff is not in the onCreate() method but in the onResume() method, because when you come back from the phone call, the onCreate() method will not be triggered but the onResume() method. See http://developer.android.com/reference/android/app/Activity.html for more details on the Activity live cycle.

  2. If that still does not help, use the flag Intent.FLAG_ACTIVITY_REORDER_TO_FRONT.

  3. Try to use a context from the listener. Either the listener directly provides a context (so you can just call startActivity(…) or call getApplicationContext().startActivity(…)

In the end your code should something like this:

Intent intent = new Intent(RS_HomeScreenActivity.this,RS_HomeScreenActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(intent);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top