I am trying to bring an activity to the front. I found many questions similar to this but none of them actually works.
I always hold a reference to the current activity in a variable in application class. While the application is running in the background (after onPause fires), if any message arrives, I need to bring the same activity to the front and display the message. The only way I got it worked is..
Intent i = new Intent(mCurrentActivity, JoboffersActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
The issue I have got with this, is that it recreates the activity which I wanted to avoid. I tried singleInstance
and singleTask
both in the manifest. If I do not add FLAG_ACTIVITY_CLEAR_TOP
, on back key press it takes me to the previous instance of the same activity. Even if I add sigleInstance
it creates two instances of the same activity. If I do not add FLAG_ACTIVITY_NEW_TASK
then it shows an error Calling startActivity()
from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK
flag