Android从通知活动导航到父活动

时间:2017-03-18 20:49:22

标签: android android-intent android-activity android-notifications navigationbar

我的问题是我有两个活动,活动A和活动B.A是主要活动,A是活动B的父活动。活动B可通过触摸通知或活动A来访问。

活动开始活动B是这样的:

            Intent intent = new Intent(getActivity(), B.class);
            startActivityForResult(intent, RESULT_ACTIVITY_1);

通知启动活动B,如下所示:

    Intent openIntent = new Intent(context, B.class);
    openIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    PendingIntent pendingIntentOpen = PendingIntent.getActivity(context, 0 , openIntent, PendingIntent.FLAG_ONE_SHOT );

    contentView.setOnClickPendingIntent(R.id.textView5NotifyOpen,pendingIntentOpen);

活动B的显示:

    <activity
        android:name=".B"
        android:parentActivityName=".MainActivity"
        android:theme="@style/Theme.AppCompat.Light.NoActionBar">
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.eee.ccc.MainActivity" />
    </activity>

当活动B关闭时发回一些数据:

        Intent returnIntent = new Intent();
        returnIntent.putExtra("Data",some data);

        setResult(Activity.RESULT_OK,returnIntent);
        finish();

到目前为止一切运作良好,我能够从A和通知中启动活动B,但是当我从通知启动它时,B终止活动A没有被调用。 现在我要做的是,当我点击通知时,它启动活动B,当B关闭/完成他的父活动A时,启动 setResult(Activity.RESULT_OK,returnIntent); 。 谢谢!

1 个答案:

答案 0 :(得分:0)

  

尝试TaskStackBuilder

    // Intent for the activity to open when user selects the notification
    Intent detailsIntent = new Intent(this, DetailsActivity.class);

    // Use TaskStackBuilder to build the back stack and get the PendingIntent
    PendingIntent pendingIntent =
            TaskStackBuilder.create(this)
                            // add all of DetailsActivity's parents to the stack,
                            // followed by DetailsActivity itself
                            .addNextIntentWithParentStack(upIntent)
                            .getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

    NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
    builder.setContentIntent(pendingIntent);
    ...

Read this android documentation for more details