I am using following code in service to open the main/launcher Activity , the code worked fine until I declared this project as library and created two other projects which use this library.
So in the onStartCommand of service this code is written.
final Notification notification = new Notification(R.drawable.ic_launcher, null, 0);
String notifTitle = "Service";
String notifMessage = "Running";
final Intent notificationIntent = new Intent(this, MainActivity.class);
notificationIntent.putExtra("extra", "value");
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
notificationIntent.setAction("android.intent.action.MAIN");
notificationIntent.addCategory("android.intent.category.LAUNCHER");
final PendingIntent contentIntent = PendingIntent
.getActivity(this, 0, notificationIntent,0);
notification.setLatestEventInfo(this, notifTitle, notifMessage, contentIntent);
startForeground(17, notification);
MainActivity.class is part of library, two projects which use this library have their main Activities MainActivityA , MainActivityB which extend MainActivity of library.
Now the problem is when I click notification of service, MainActivityA or MainActivityB should be launched but right now nothing happens, but previously it worked when library was a project itself
Any ideas would be appreaciated alot ,
Thank you,
There are a couple of ways to go about this.
You could declare an Intent Filter on an Activity in your manifest and broadcast the Intent, but this could be problematic if the user has both versions installed on their device. (There is a local broadcast in the support library that you could use instead docs)
Personally I would use Android’s resource system to load the class name dynamically. Intents give you that ability to set the ComponentName which is the same thing as giving it a hardcoded reference the an Activity’s class.
The library project’s values can be overridden by the projects that use them, so in both of these projects you have a value in strings.xml that corresponds to that app’s notification activity.
You can then use this Intent with your pendingIntent and it will open the activity inside of the current application.