Nick Friskel Nick Friskel - 9 days ago 5
Android Question

Correct way to restart activity after phone call ends

Both of these methods work, but i'm not sure which would be the best code to use so that it works every time for API 17-25. My app relies on the activity opening after the call ends which I do through a PhoneStateListener.

I have seen both ways recommended, sorry if this is a bad question!

Intent restart = mContext.getPackageManager().
getLaunchIntentForPackage(mContext.getPackageName());
restart.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
restart.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(restart);


or

Intent intent = new Intent(mContext, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
mContext.startActivity(intent);

Answer

You should use recreate() method of Activity class to restart Activity instead of this 2 methods