androidandroid-intentandroid-activitylaunching-application

Launch activity from background app


I have my app running in the background and I want the app to be shown on the top(launched) of the android phone when the code below is ran. (I know the code is ran for sure)

This seems like a simple thing but I spent a couple hours on this site and everyone seems to be suggesting something like this:

Intent intent = new Intent(myActivity.this, myActivity.class);
startActivity(intent);

However, it is not bringing the app to the front and launching it.

I got it to work from a PendingIntent launched from a notification. Which I done by the code below. But I want the app to launch by itself without the user clicking on the notification.

Intent intent = new Intent(myActivity.this, myActivity.class);
PendingIntent contentIntent = PendingIntent.getActivity(this, REQUEST_CODE, intent, 0);
notification.setLatestEventInfo(this, "title", "msg", contentIntent);

I also tried:

Intent intent = new Intent("android.intent.action.MAIN");
startActivity(intent);

and flagging the intent:

intent.setFlags(Intent.FLAG_FROM_BACKGROUND);
intent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

But doesn't seem to do anything, any help appreciated.


Solution

  • I ended up using a pending intent and instead of stright up trying to use a intent. Something like this: seems a lot more simple.

    Intent.send(this, 0, intent);

    Thanks.