After an activity is launched from notification lets say activity2(result) get finished or destoryed when user uses backpress its is going to homescreen instead of Main activity.How to prevent this and open the mainactivity.
As of now the method implement is to check if the activity2 is launched from mainactivity(using extras) directly or from notifications so that the onDestory is controlled based on bundle values but the this a noticeable time difference is the a rectify this or create a stack history when the notification is launched
current code
notificationservice.java
Intent resultIntent = new Intent(this, videonotifications.class);
Intent parentIntent=new Intent(this, MainActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
//stackBuilder.addNextIntentWithParentStack(resultIntent);
stackBuilder.addParentStack(videonotifications.class);
// Adds the Intent to the top of the stack
stackBuilder.addNextIntent(resultIntent);
PendingIntent resultPendingIntent =stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
mNotificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
createNotificationChannel(mNotificationManager);
Notification notification = new NotificationCompat.Builder(this,DEFAULT_CHANNEL_ID)
.setContentTitle("Fall Detected")
.setStyle(new NotificationCompat.BigTextStyle()
.bigText("A Fall Has Been Detected By Smart Video Surveillance At Old Age Home's Common Room"))//Set the title of Notification
.setSmallIcon(R.drawable.ic_home_black_24dp)
.setAutoCancel(true)
.setContentIntent(resultPendingIntent)
.build();
mNotificationManager.notify(1, notification);
final Intent i=new Intent(this,notificationsservice.class);
//stopService(i);
videnotification.java
public void onDestroy() {
//to be optimized as the roundabout procedure
super.onDestroy();
Bundle extras;
extras=getIntent().getExtras();
if(extras==null){
Intent i=new Intent(this,MainActivity.class);
i.putExtra("activity","notification is already called");
startActivity(i);
}
}
MainActivity.java
Bundle extras=null;
extras=getIntent().getExtras();
if(extras==null){
final Intent i=new Intent(this, notificationsservice.class);
new Timer().schedule(new TimerTask() {
@Override
public void run() {
startService(i);
}
},5000);}
AndroidManifest.xml
<activity
android:name=".videonotifications"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>