Android 导航回父活动

Android 导航回父活动,android,notifications,Android,Notifications,我正试图从工具栏中的“向上导航”按钮移回父活动。以下是我的代码: @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: Intent upIntent = NavUtils.getParentActivityIntent(this);

我正试图从工具栏中的“向上导航”按钮移回父活动。以下是我的代码:

    @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
}
现在的问题是upIntent始终为空。我想在从通知打开活动时导航回父活动。 更新: 以下是我制作通知的方式:

 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());
和我的清单文件:

   <activity
        android:name="com.myblinddate.Myactivity"
        android:parentActivityName=".Parentactivity" />

尝试在清单文件中添加父活动,如下面的代码所示

<activity android:name=".LoginActivity"
     android:parentActivityName=".SignupActivity">
</activity>
或者像这样在你的方法中使用这个

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

尝试在清单文件中使用以下类似代码的
android:parentActivityName
添加父活动

<activity android:name=".LoginActivity"
     android:parentActivityName=".SignupActivity">
</activity>
或者像这样在你的方法中使用这个

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

这可能是您的解决方案,您可以简单地处理back事件,而不是查找父活动

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

这可能是您的解决方案,您可以简单地处理back事件,而不是查找父活动

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case android.R.id.home:
                super.onBackPressed();
               // or
               //finish();
                break;
} return true;
}
//包括以下代码

@Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                break;
        }
        return true;
    }
如果有任何困难,请返回

//包括以下代码

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

如果有任何困难,请返回。

在清单中的活动定义中添加元数据标记,如下所示:

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

然后调用
Intent-upIntent=NavUtils.getParentActivityIntent(this)将不会返回null


希望有帮助。

在清单中的活动定义中添加元数据标记,如下所示:

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

然后调用
Intent-upIntent=NavUtils.getParentActivityIntent(this)将不会返回null


希望有帮助。

不过,通过通知打开活动时,我无法移动到父活动。是的,我有added@Roasterbhusri检查一下这个,通过通知打开活动时,我无法移动到父活动。是的,我有added@Roasterbhusri选中此复选框,当尝试通过通知打开活动时,我仍然无法后退。当尝试通过通知打开活动时,我仍然无法后退。当尝试打开活动时,我仍然无法后退通过通知发送一些来自通知的附加内容并检查是否来自通知,然后完成此操作并打开父活动。在尝试通过通知打开活动时,我仍然无法后退发送一些来自通知的附加内容并检查是否来自通知,然后完成此操作并打开父活动。尝试此
resultEnt4.setFlags(Intent.FLAG\u activity\u MULTIPLE\u TASK)
并执行应用程序的干净安装尝试此
resultEnt4.setFlags(Intent.FLAG\u activity\u MULTIPLE\u TASK)
并执行应用程序的干净安装