导航回父活动

时间:2017-09-04 10:39:48

标签: android notifications

我正在尝试从工具栏中的向上导航按钮返回到父活动。这是我的代码:

    @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始终为null。我想在通知打开活动时导航回父活动。 更新: 这是我如何制作通知:

 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" />

4 个答案:

答案 0 :(得分: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>

//在java代码中使用onCreate方法

getSupportActionBar().setDisplayHomeAsUpEnabled(true);

//包括以下代码

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

如果您有任何困难,请退回。

答案 1 :(得分:0)

尝试使用android:parentActivityName(如下面的代码

)在清单文件中添加父活动
<activity android:name=".LoginActivity"
     android:parentActivityName=".SignupActivity">
</activity>

并将其添加到您的活动文件

  getSupportActionBar().setDisplayHomeAsUpEnabled(true);

或将此用于您的方法

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

答案 2 :(得分:0)

这可能是您的解决方案,您只需处理回事件而不是查找父活动。

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

答案 3 :(得分:0)

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

 <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。

希望它有所帮助。