首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >在Android上进行父级活动

在Android上进行父级活动
EN

Stack Overflow用户
提问于 2014-04-27 20:07:14
回答 7查看 6.4K关注 0票数 13

在我的应用程序中收到通知后,单击它打开活动B。活动B有一个父活动A。下面是清单:

代码语言:javascript
运行
复制
<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功能(活动操作的条形图附近的左箭头),下面是代码:

代码语言:javascript
运行
复制
@Override
protected void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    getActionBar().setDisplayHomeAsUpEnabled(true);
    ...

问题是,如果活动B是通过单击通知打开的(活动A不是带来活动B的那个),那么当单击图标时,应用程序就关闭了。我想把它打开,这是家长的活动,可以吗?还是应该用活动B中的startActivity()来做呢?

更新1-我添加了以下代码:

代码语言:javascript
运行
复制
@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;

谢谢

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2014-05-05 07:12:55

您需要设置用于构建PendingIntentNotification,启动一个新任务,并为PendingIntent提供一个back stack,以实现应用程序正常的up行为。

代码语言:javascript
运行
复制
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);

请阅读启动活动时保持导航上的安卓官方文档。委员会建议采用上述办法。

票数 12
EN

Stack Overflow用户

发布于 2014-05-05 07:50:07

我使用了下面的代码,它的工作原理就像一种魅力。试一试!

代码语言:javascript
运行
复制
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();
}
票数 1
EN

Stack Overflow用户

发布于 2014-05-05 12:49:46

创建一个临时活动,以便在单击通知时启动(不使用setContentView)。在那里,您可以决定启动哪一项活动。取决于你的逻辑。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/23328367

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档