Not Navigating back to MainActivity

86 Views Asked by At

When I click on Notification I should be navigated to Main2Activity and when I click on back button of Main2Activity I should be navigated back to MainActivitybut I am getting navigated back to Home screen. Is there any mistake in my code?

enter image description here

            NotificationCompat.Builder  noti = new NotificationCompat.Builder(MainActivity.this);
            noti.setContentTitle("Message for you!");
            noti.setContentText("Hi!!This is message for you");
            noti.setSmallIcon(R.drawable.ic_launcher_background);
            noti.setTicker("app name:message app");
            noti.setAutoCancel(true);


            Intent intent = new Intent(MainActivity.this,Main2Activity.class);

            TaskStackBuilder taskStackBuilder=TaskStackBuilder.create(MainActivity.this);
            taskStackBuilder.addParentStack(MainActivity.class);
            taskStackBuilder.addNextIntent(intent);

            PendingIntent pendingIntent= 
  taskStackBuilder.getPendingIntent(1234,PendingIntent.FLAG_UPDATE_CURRENT);
            noti.setContentIntent(pendingIntent);

            Notification notification=noti.build();
            NotificationManager notificationManager = (NotificationManager) 
            getSystemService(NOTIFICATION_SERVICE);
            notificationManager.notify(1234,notification);

Mainifest.XML file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sainathpawar.notifications">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity android:name=".Main2Activity"
        android:parentActivityName=".MainActivity">

        <intent-filter>
            <action android:name="second_filter" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
</application>

4

There are 4 best solutions below

3
On

It is because you are not within a stack. You only launched one activity. Your default MainActivity has not been created or launched.

You can handle the back button press with android.R.id.home in the OnMenuItemSelected callback and redirect them wherever you would like. You can try the "parent activity" route as well, but I'm not certain how that works when launched from notification without context of the parent to launch it to begin with.

If you go that route, update to let us know if it worked for you. Otherwise you can easily use my answer as well.

EDITED FOR CLARITY

  @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                startActivity(Main2Activity.this, MainActivity.class);
                return true;
                finish();
        }

        return super.onOptionsItemSelected(item);
    }
0
On

You can achieve this by PendingIntent without TaskStackBuilder as:

Intent parentIntent = new Intent(this, MainActivity.class);
parentIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
Intent resultIntent = new Intent(this, Main2Activity.class);

final PendingIntent pendingIntent = PendingIntent.getActivities(context, 0,
                new Intent[] {parentIntent, resultIntent}, PendingIntent.FLAG_UPDATE_CURRENT);

noti.setContentIntent(pendingIntent);
0
On

taskStackBuilder.getPendingIntent(1234,PendingIntent.FLAG_UPDATE_CURRENT); noti.setContentIntent(pendingIntent);

There was error in coding at above line.If we see here the requestcode is 1234 which is same as ID of notify method

notificationManager.notify(1234,notification);

so it was navigating back to Home screen because Android OS was thinking as if its on MainActivity because of 1234 request code in getPendingIntent.

****Solution is:****change requestCode of getPendingIntent() method from 1234 to any random number I changed it to 0 and it worked for me.

1
On

Please try this

    <activity
        android:name=".Main2Activity"
        android:parentActivityName=".MainActivity">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".MainActivity" />
    </activity>