Search code examples
androidandroid-activityphone-state-listener

Starting an Activity does not put application in foreground


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();
            }
        }
    }
}

Solution

  • 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));
        }
    }