Launch activity from background app

zergy picture zergy · May 24, 2012 · Viewed 21k times · Source

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.

Answer

Bram P. picture Bram P. · May 24, 2012

You should be able to call your own application like this:

Intent intent = new Intent("android.intent.category.LAUNCHER");
intent.setClassName("com.your.package", "com.your.package.MainActivity");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);

Edit: Forgot to add intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);