我从通知中启动了一项活动。我使用TaskStackBuilder包含一个后栈,这样当用户点击主页按钮(操作栏标题按钮)或使用后退键时,它将返回到应用程序。但是,它不是以这种方式工作,而是回击或操作栏标题按钮总是导致应用程序关闭。
对于它的价值,我的项目结构是有组织的,所有UI组件都在Android库(com.be.commotion)中,外部“包装”项目使用该库。
以下是我如何构建通知:
// This is the activity that will be launched when tapping the notification
Intent intentNotification = new Intent(this, NowPlaying.class);
intentNotification.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
// Create the back stack so the user can get back to the main activity when pressing the back button
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
stackBuilder.addParentStack(NowPlaying.class);
stackBuilder.addNextIntent(intentNotification);
PendingIntent pendingNowPlayingIntent = stackBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
// Setup the custom notification view
RemoteViews notificationContent = new RemoteViews(getPackageName(), R.layout.notification_now_playing);
....
// Setup the intent that will play/stop music when the stop button is tapped
Intent musicControlIntent = new Intent(this, CommotionMediaPlayer.class);
PendingIntent musicPendingIntent = PendingIntent.getService(this, 0, musicControlIntent, PendingIntent.FLAG_UPDATE_CURRENT);
notificationContent.setOnClickPendingIntent(R.id.ibMediaControl, musicPendingIntent);
// Update the notification with this info
NotificationCompat.Builder builder = new NotificationCompat.Builder(this)
.setSmallIcon(R.drawable.ic_launcher)
.setContentTitle(song.text)
.setContentText(song.artist)
.setContentIntent(pendingNowPlayingIntent)
.setLargeIcon(artwork)
.setContent(notificationContent)
.setOngoing(true);
NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
// Using the nowPlayingNotificationId allows the notification to be updated with later calls instead of causing a new notification to show up
mNotificationManager.notify(nowPlayingNotificationId, builder.build());
以下是我的AndroidManifest.xml中的适用定义(对于包装器项目):
<activity android:name="com.be.commotion.ui.StartupActivity"
android:label="@string/app_name"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="@android:style/Theme.Holo.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name="com.be.commotion.ui.NowPlaying"
android:label="Now Playing"
android:parentActivityName="com.be.commotion.ui.StartupActivity"
android:screenOrientation="portrait">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.be.commotion.ui.StartupActivity" />
</activity>
答案 0 :(得分:3)
你可以在不使用TaskStackBuilder
的情况下通过2步完成
1 /为你的意图设置标志
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK);
2 /为PendingIntent设置标志
pendingIntent = PendingIntent.getActivity(MainActivity.this,0,intent,PendingIntent.FLAG_CANCEL_CURRENT);
它适合我,祝你好运
答案 1 :(得分:1)
在添加所需活动(单击通知时直接调用的活动)之前,将主活动添加到堆栈。 例如:
stackBuilder.addNextIntent(new Intent(this,MainActivity.class));
stackBuilder.addNextIntent(new Intent(this,DesiredActivity.class));
答案 2 :(得分:0)
也许我们正在解决一个稍微不同的问题,但它归结为同一个问题。对于我们的解决方案是使用:
startActivityForResult(intent, 0);
我们想要做的是有一个通知,告知需要到活动B和,返回按钮被点击时,要带我们去活动A
有两种方法可以做到这一点: