gpt4 book ai didi

java - TaskStackBuilder 未返回 MainActivity

转载 作者:行者123 更新时间:2023-12-02 11:41:57 25 4
gpt4 key购买 nike

所以我正在研究 Android 通知,

我在 android documentation 上尝试了一个通知示例:

在其中一个示例中,它展示了如何通过通知打开 Activity 。

所以问题是:当我尝试按通知打开的 Activity 上的后退按钮时,它应该返回到 MainActivity 但应用​​程序关闭。

ResultActivity 只是带有 TextView 的默认空 Activity

MainActivity.java:

// The id of the channel.

String CHANNEL_ID = "my_channel_01";

NotificationCompat.Builder mBuilder =
new NotificationCompat.Builder(this, CHANNEL_ID)
.setSmallIcon(android.R.drawable.stat_notify_more)
.setContentTitle("Event tracker")
.setContentText("Events received");

NotificationCompat.InboxStyle inboxStyle =
new NotificationCompat.InboxStyle();

String[] events = new String[6];

// / Sets a title for the Inbox in expanded layout
inboxStyle.setBigContentTitle("Event tracker details:");

// Creates an explicit intent for an Activity in your app
Intent resultIntent = new Intent(this, ResultActivity.class);

TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

// Adds the back stack for the Intent (but not the Intent itself)
stackBuilder.addParentStack(MainActivity.class);

// Adds the Intent that starts the Activity to the top of the stack
stackBuilder.addNextIntent(resultIntent);

PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);

// Moves events into the expanded layout
for (int i=0; i < events.length; i++) {
events[i] = "Event " + i;
inboxStyle.addLine(events[i]);
}

// Moves the expanded layout object into the notification object.
mBuilder.setStyle(inboxStyle);

mBuilder.setContentIntent(resultPendingIntent);
NotificationManager mNotificationManager =
(NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);


mNotificationManager.notify(1, mBuilder.build());

AndroidManifest.xml:

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

<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=".ResultActivity"
android:parentActivityName=".MainActivity"
/>
</application>

</manifest>

最佳答案

替换行:

stackBuilder.addParentStack(MainActivity.class);

与:

stackBuilder.addParentStack(ResultActivity.class);

同时添加 <meta-data如果您在低于 16 的 API 级别上进行测试,请在 list 中标记您的 Activity

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

关于java - TaskStackBuilder 未返回 MainActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48480362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com