1

I am trying to move back to parent activity from navigate up button in the toolbar.here is my code:

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
                Intent upIntent = NavUtils.getParentActivityIntent(this);
                if (upIntent != null)
                    if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
                        // This activity is NOT part of this app's task, so create a new task
                        // when navigating up, with a synthesized back stack.
                        TaskStackBuilder.create(this)
                                // Add all of this activity's parents to the back stack
                                .addNextIntentWithParentStack(upIntent)
                                // Navigate up to the closest parent
                                .startActivities();
                    } else {
                        // This activity is part of this app's task, so simply
                        // navigate up to the logical parent activity.
                        NavUtils.navigateUpTo(this, upIntent);
                    }

                break;
} return true
}

now the problem is upIntent is always null.I want to navigate back to parent activity when activity is opened from notification. UPDATE: here is how i am producing notification:

 NotificationCompat.Builder mBuilder4 =
                new NotificationCompat.Builder(this)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle("Someone")
                        .setPriority(Notification.PRIORITY_MAX)
                        .setAutoCancel(true)
                        .setContentText(map.get("text"));
        Intent resultIntent4 = new Intent(this, Myactivity.class);
        resultIntent4.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        TaskStackBuilder stackBuilder4 = TaskStackBuilder.create(this);
        stackBuilder4.addParentStack(Myactivity.class);
        stackBuilder4.addNextIntent(resultIntent4);
        PendingIntent resultPendingIntent4 =
                stackBuilder4.getPendingIntent(
                        0,
                        PendingIntent.FLAG_UPDATE_CURRENT
                );
        mBuilder4.setContentIntent(resultPendingIntent4);
        SharedPref pref = new SharedPref();
        int soundstatus = pref.getSoundStatus(getApplicationContext());
        if (soundstatus == 1)
            mBuilder4.setDefaults(Notification.DEFAULT_SOUND);
        NotificationManager mNotificationManager4 =
                (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
        mNotificationManager4.notify(102, mBuilder4.build());

and my manifest file :

   <activity
        android:name="com.myblinddate.Myactivity"
        android:parentActivityName=".Parentactivity" />
Roaster bhusri
  • 53
  • 1
  • 10

4 Answers4

2
<activity
    android:name="activityName"
    android:parentActivityName=".activity.ServiceDetailsActivity"
        <meta-data
             android:name="android.support.PARENT_ACTIVITY"
             android:value="parent activity name with package" />
 </activity>

// In java code use in onCreate method

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

// Include following code

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

Revert back if you have any difficulty.

lalit jadhav
  • 320
  • 1
  • 4
  • 13
0

try this add parent activity in your manifest file using android:parentActivityName like below code

<activity android:name=".LoginActivity"
     android:parentActivityName=".SignupActivity">
</activity>

and add this in to your activity file

  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

or use this in to your method like this

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
                super.onBackPressed();
                break;
} return true;
}
AskNilesh
  • 67,701
  • 16
  • 123
  • 163
0

This may be your solution, you can simply handle back event instead of finding parent activity.

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
                super.onBackPressed();
               // or
               //finish();
                break;
} return true;
}
Amrish Kakadiya
  • 974
  • 1
  • 7
  • 25
0

Add meta-data tag in activity definition in Manifest like this :

 <activity
        android:name=".Myactivity"
        android:label="@string/app_name">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value=".Parentactivity"/>
    </activity>

Then calling Intent upIntent = NavUtils.getParentActivityIntent(this); will not return null.

Hope it helps.

Kapil Rajput
  • 11,429
  • 9
  • 50
  • 65