问题描述
在我的应用程序中收到通知后,单击它会打开活动B.活动B具有父活动A.这是清单:
After receiving a notification in my app, clicking on it opens activity B. Activity B has a parent activity A. Here is the manifest:
<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中,我启用了向上功能(活动动作的条形图标附近的向左箭头),这是代码:
In activity B I have the up functionality enabled (left arrow near the activity action's bar icon), here is the code:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
getActionBar().setDisplayHomeAsUpEnabled(true);
...
问题是,如果通过单击通知打开了活动B(活动A不是带来活动B的活动),则在单击该图标时会关闭该应用程序.我想将其打开为父活动A.可以吗?还是应该用活动B中的startActivity()
来做?
The problem is that if activity B was opened via clicking on the notification (activity A was not the one to bring activity B) the when clicking on the icon the app is closed. I would like to make it open it's parent activity, A. Is it possible? or should I do it with startActivity()
from activity B?
更新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;
谢谢
推荐答案
您需要设置用于构建Notification
的PendingIntent
,开始新任务,并为PendingIntent
提供back stack
实现应用程序的正常Up行为.
You need to set up the PendingIntent
which is used to build Notification
, to start a fresh task, and provide the PendingIntent
with a back stack
to achieve the application's normal Up behavior.
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);
请阅读 开始活动时保留导航的Android官方文档 .建议采用上述方法.
Please read the Android official documentation on Preserving Navigation when Starting an Activity. It recommends the above approach.
这篇关于取决于家长的活动-在Android上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!