0

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

ArtKorchagin
  • 4,801
  • 13
  • 42
  • 58
Anup
  • 115
  • 5
  • 15
  • Why can't you show a notification? I would be very annoyed if an app suddenly opens itself in front of whatever I was doing. – Karakuri Nov 15 '15 at 22:27

1 Answers1

0

Try to add another flag FLAG_ACTIVITY_SINGLE_TOP to your intent. According to Android documentation, combination of this 2 flags:

i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);

brings the current instance of Activity to the front. http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP

Ewa Kropkowska
  • 103
  • 1
  • 8