1

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>
  • refer [this](https://stackoverflow.com/questions/13800680/back-to-main-activity-from-notification-created-activity) – Jimale Abdi Oct 27 '19 at 13:59

2 Answers2

1

TL;DR - Override onBackPressed() [The method called when a user presses the back button], and if the user has come to the activity from a notification, then start MainActivity using an intent.

My understanding is that you want to move from activity 2 (Let's call this FromNotificationActivity) to activity 1 (Let's call this MainActivity) only when FromNotificationActivity is started by clicking a notification, i.e.:

User clicks notification ---> FromNotificationActivity is started ---> Back button is pressed and FromNotificationActivity is destroyed ---> Instead of returning to the home screen, you want to start MainActivity.

I believe the quickest way to do this is by overriding onBackPressed() and going directly to MainActivity only if FromNotificationActivity is started from a notification.

So, when you are creating the notification, add this line -

intent.putExtra("origin_from_notification", true);

So your notification builder will look like this -

Intent intent = new Intent(context, FromNotificationActivity.class);
intent.putExtra("origin_from_notification", true);
PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, 0);

// TODO: Replace with .Build() for api >= 16
Notification notification = new Notification.Builder(context)
    .setContentTitle("Notification Title"
    .setContentText("Notification text")
    .setSmallIcon(R.drawable.icon)
    .setContentIntent(pendingIntent)
    .// Whatever more you want to add

Then, in your FromNotificationActivity, after onCreate(), add the following lines -

@Override
public void onBackPressed() {
    // Check if the user came here from a notification
    if (getIntent().getBooleanExtra("origin_from_notification", true)) {
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    } else {
        // Do whatever you want, the user came here from another screen
    }
}

And optionally, if your activity has a back button in the title bar, use this -

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            break;
    }
}

Hope this helps!

Gamemaker
  • 64
  • 5
0

First add activity2 the android:parentActivityName attribute at Manifest

<activity
    android:name=".Activity2"
    android:parentActivityName=".MainActivity" />

Then to start an activity that includes a back stack of activities, you need to create an instance of TaskStackBuilder and call addNextIntentWithParentStack(), passing it the Intent for the activity you want to start. you can call getPendingIntent() to receive a PendingIntent that includes the entire back stack.

// Create an Intent for the activity you want to start
Intent resultIntent = new Intent(this, Activity2.class);
// Create the TaskStackBuilder and add the intent, which inflates the back stack
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addNextIntentWithParentStack(resultIntent);
// Get the PendingIntent containing the entire back stack
PendingIntent resultPendingIntent =
        stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

Then you can pass the PendingIntent to the notification as usual:

NotificationCompat.Builder builder = new NotificationCompat.Builder(this, CHANNEL_ID);
builder.setContentIntent(resultPendingIntent);
...
NotificationManagerCompat notificationManager = NotificationManagerCompat.from(this);
notificationManager.notify(NOTIFICATION_ID, builder.build());
Jimale Abdi
  • 2,574
  • 5
  • 26
  • 33