TaskStackBuilder没有返回MainActivity

时间:2018-01-27 20:44:38

标签: java android xml

所以我正在研究Android通知,

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

在其中一个示例中,显示了如何从通知中打开活动。

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

ResultActivity只是具有TextView

的默认空活动

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>

1 个答案:

答案 0 :(得分:1)

替换行:

         [TestFixture]
        class StudyLoaderTest
        {
            [Test]
            public void Test()
            {
                using (var mock = AutoMock.GetLoose())
                {
                    // The AutoMock class will inject a mock IStudyLoader
                    // into the StudyLoader constructor
                    //no need to create/configure a container
                    var studyLoader = mock.Create<StudyLoader>();               
                    Assert.AreEqual("hi AutoFac Moq", studyLoader.Name);
                }
            }
        }

         class StudyLoader : IStudyLoader
        {
            public string Name { get; set; } = "hi AutoFac Moq";
        }
         interface IStudyLoader
        {
            string Name { get; set; }
        }

使用:

stackBuilder.addParentStack(MainActivity.class);

如果您在API级别低于16的情况下进行测试,请在清单中为您的活动添加stackBuilder.addParentStack(ResultActivity.class); 标记

<meta-data