17

I am trying to start the main activity from inside a BroadcastReceiver. I dont want to supply the activity class name but to use the action and category for android to figure out the main activity.

It doesnt seem to work.

Sending Code:

Intent startIntent = new Intent();

startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startIntent.setAction(Intent.ACTION_MAIN);
startIntent.setPackage(context.getPackageName());
startIntent.addCategory(Intent.CATEGORY_LAUNCHER);        
context.startActivity(startIntent);

I get this error:

Caused bt: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 pkg=com.xyz.abc (has extras) }

Any ideas?

Armand
  • 23,463
  • 20
  • 90
  • 119
Abhishek
  • 1,749
  • 9
  • 27
  • 39

4 Answers4

26

Copy from another topic:

This works since API Level 3 (Android 1.5):

private void startMainActivity(Context context) throws NameNotFoundException {
    PackageManager pm = context.getPackageManager();
    Intent intent = pm.getLaunchIntentForPackage(context.getPackageName());
    context.startActivity(intent);
}
TienLuong
  • 1,065
  • 1
  • 10
  • 7
11

this is not the right way to startActivity.
try this code instead:

Intent startIntent = new Intent(context, MainActivity.class);
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);        
context.startActivity(startIntent);
Tal Kanel
  • 10,475
  • 10
  • 60
  • 98
  • I am having only the context of application instead of MainActivity.class in the library project. How can I start the application's main activity from the library project.? – Karthick Sep 17 '13 at 07:02
  • @Karthick: there is no problem using application context instead of MainActivity.class, but - if you won't use the Intent.FLAG_ACTIVITY_NEW_TASK flag, you'll get an exception in that case, because starting activity from application/Service context requires this flag. – Tal Kanel Sep 17 '13 at 07:16
  • If I am using context means, How can I use? Intent notificationIntent = new Intent(context, (Activity)(PushNotification.mActiveContext)); getting error. I need this in the below link scenario. http://stackoverflow.com/questions/18843118/launch-android-application-from-library-project – Karthick Sep 17 '13 at 07:20
  • @Karthick: I've seen your question, and if I understood currectly - I provided a good unswer – Tal Kanel Sep 17 '13 at 08:12
  • Intent startIntent = new Intent(getApplicationContext(), MainActivity.class); startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); getApplicationContext().startActivity(startIntent); – Alexandros Kourtis Jun 06 '22 at 08:29
2

Even I had been trying to launch the MainActivity via a library Activity.

And this worked for me:

Intent startIntent = new Intent();
startIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startIntent.setPackage(getApplicationContext().getPackageName());
getApplicationContext().startActivity(startIntent);

Make sure you add the activity in your library's manifest!

Arnab Saha
  • 511
  • 6
  • 15
  • And here is exception when I tried the same: android.content.ActivityNotFoundException: No Activity found to handle Intent – TienLuong Sep 21 '15 at 12:33
1

Even though it is too late. Might be useful for somone in the future. This helped me.

Intent intent = new Intent(this, MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
cantona_7
  • 1,127
  • 4
  • 21
  • 40