Reputation: 3824
I am working on a application where user can login in the login activity. And a logged in user can see their review in dashboard activity. And if any new review is coming in server it will push a notification. Everything is running perfect Except one thing i.e. when user click on the notification, it is opening a new dashboard activity every time if the dashboard activity is in front then also.
What i am wanting is that if user click on the notification it will open dashboard activity only when application is not running. otherwise if the dashboard activity is in front then if user will click on it , it will first close the dashboard activity and the it will reopen the activity page. Here is the code i am written to go the dashboard activity.
Intent startActivityIntent = new Intent(this, DashboardActivity.class);
startActivityIntent.putExtra("NotificationMessage", service_notification);
startActivityIntent.putExtra("flag", 1);
startActivityIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK| Intent.FLAG_ACTIVITY_NEW_TASK );
PendingIntent launchIntent = PendingIntent.getActivity(this, 0,
startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
earthquakeNotificationBuilder
.setContentIntent(launchIntent)
.setContentTitle(no_of_review + " " + "new review is there")
.setAutoCancel(true);
I have tried many thing but didn't get a solution. Can anyone help me? Thanks in advance.
Upvotes: 2
Views: 5341
Reputation: 755
THIS CODE WILL reopen a activity if open,otherwise app will start again
ActivityManager am = (ActivityManager)context.getSystemService(context.ACTIVITY_SERVICE);
List<ActivityManager.RunningTaskInfo> tasks = am.getRunningTasks(1);
ActivityManager.RunningTaskInfo task = tasks.get(0); // get current task
ComponentName rootActivity = task.baseActivity;
Intent notificationIntent;
if(rootActivity.getPackageName().equalsIgnoreCase("your package name")){
//your app is open
// Now build an Intent that will bring this task to the front
notificationIntent = new Intent();
notificationIntent.setComponent(rootActivity);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
}
else
{
//your app is not open,start it by calling launcher activity
notificationIntent = new Intent(context, SplashActivity.class);
}
notificationIntent.setAction(Intent.ACTION_MAIN);
notificationIntent.addCategory(Intent.CATEGORY_LAUNCHER);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
PendingIntent intent = PendingIntent.getActivity(context, 0,
notificationIntent, 0);
//use this pending intent in notification..
see complete example HERE ..
Upvotes: 1
Reputation: 8358
What you are doing is that you are creating a new activity every time the user clicks the notification. Now the thing you have to do is add this FLAG_ACTIVITY_CLEAR_TOP to your intent and also change launchmode of your activity in ManifestFile. Also Override OnNewIntent() Method in your DashboardActivity
Here is some Code to help you
startActivityIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
And Change your ManifestFile like this
<activity
android:name=".DashboardActivity"
android:launchMode="singleTop">
</activity>
Then Override OnNewIntent Method in your DashBoard Activity like this
@Override
protected void onNewIntent(Intent intent) {
DashboardActivity.this.finish();
// -----Start New Dashboard when notification is clicked----
Intent i = new Intent(DashboardActivity.this, DashboardActivity.class);
startActivity(i);
super.onNewIntent(intent);
}
The OnNewIntent method will fire whenever your activity exists and notification is clicked
Also study launchmodes if you want your activities to behave differently in different situations.
Upvotes: 0
Reputation: 2702
and the android:launchMode="singleTask"
in your androidmanifest.xml so you can andd android:clearTaskOnLaunch="true
<activity
android:name=".DashboardActivity"
android:launchMode="singleTask"
android:clearTaskOnLaunch="true" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Upvotes: 3