首页 > 解决方案 > 使用 Intents 时回栈不断添加

问题描述

我有一个活动 A,它使用具有列表的片段 A。活动 A 可以调用搜索活动。我看到的问题是,如果我要进行搜索活动,然后返回活动 A,则从那里加载片段 A。如果我从片段 A 中选择一个项目,我会被带到片段 B,如果我要按下后退按钮,我必须单击它 2 或 3 次。有任何想法吗?我是否需要使用参数启动搜索活动,以便它不会添加到后台堆栈。我在开始搜索活动时尝试了 Intent.FLAG_ACTIVITY_CLEAR_TOP 的标志,但出现了问题。

爪哇:

活动一:

public void popFragment(){
    if(getSupportFragmentManager().getBackStackEntryCount() > 1) {
        getSupportFragmentManager().popBackStack();
        getSupportFragmentManager().executePendingTransactions();
    }
}

片段 A:

private void showSearchActivity(){
    Intent intent = new Intent(context, SearchActivity.class);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    startActivityForResult(intent, 0)
}

标签: androidandroid-fragmentsandroid-activityfragment-backstack

解决方案


推荐阅读