在我的应用程序中收到通知后,单击它打开活动B。活动B有一个父活动A。下面是清单:
<activity
android:name="com.evapp.activities.B"
android:label="@string/title_activity_B"
android:parentActivityName="com.evapp.activities.A"
android:screenOrientation="portrait" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.evapp.activities.A" />
</activity>在活动B中,我启用了up功能(活动操作的条形图附近的左箭头),下面是代码:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
...问题是,如果活动B是通过单击通知打开的(活动A不是带来活动B的那个),那么当单击图标时,应用程序就关闭了。我想把它打开,这是家长的活动,可以吗?还是应该用活动B中的startActivity()来做呢?
更新1-我添加了以下代码:
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
switch (item.getItemId())
{
case android.R.id.home:
Intent upIntent = NavUtils.getParentActivityIntent(this);
if (NavUtils.shouldUpRecreateTask(this, upIntent))
{
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(upIntent)
.startActivities();
}
else
{
NavUtils.navigateUpTo(this, upIntent);
}
return true;谢谢
发布于 2014-05-05 07:12:55
您需要设置用于构建PendingIntent的Notification,启动一个新任务,并为PendingIntent提供一个back stack,以实现应用程序正常的up行为。
Intent resultIntent = new Intent(this, SecondActivity.class);
TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);
// All the parents of SecondActivity will be added to task stack.
stackBuilder.addParentStack(SecondActivity.class);
// Add a SecondActivity intent to the task stack.
stackBuilder.addNextIntent(resultIntent);
// Obtain a PendingIntent for launching the task constructed by this builder.
PendingIntent pendingIntent = stackBuilder.getPendingIntent(REQUEST_CODE, PendingIntent.FLAG_UPDATE_CURRENT);
NotificationManager manager = (NotificationManager) this.getSystemService(NOTIFICATION_SERVICE);
Notification notification = new Notification.Builder(this)
.setContentTitle("My Notification")
.setContentText("Notification content")
.setSmallIcon(android.R.drawable.ic_menu_view)
.setContentIntent(pendingIntent)
.build();
manager.notify(NOTIFICATION_ID, notification);请阅读启动活动时保持导航上的安卓官方文档。委员会建议采用上述办法。
发布于 2014-05-05 07:50:07
我使用了下面的代码,它的工作原理就像一种魅力。试一试!
Intent upIntent = new Intent(getApplicationContext(), Home.class);
if (NavUtils.shouldUpRecreateTask(this, upIntent)) {
Log.d("ShowNotifications", "New Home");
TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities();
} else {
Log.d("ShowNotifications", "Old Home");
upIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_SINGLE_TOP);
//upIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK );
startActivity(upIntent);
finish();
}发布于 2014-05-05 12:49:46
创建一个临时活动,以便在单击通知时启动(不使用setContentView)。在那里,您可以决定启动哪一项活动。取决于你的逻辑。
https://stackoverflow.com/questions/23328367
复制相似问题