在BackPressed上跳过一些片段 [英] Skip some fragments onBackPressed

查看:45
本文介绍了在BackPressed上跳过一些片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对android片段还比较陌生,所以请多多包涵.

I am fairly new with android fragments so please bear with me.

我有一堆使用单个活动作为主机的片段.

I have a bunch of fragments using a single activity as host.

在我看来,我的片段按部分分组,尽管它们仍然是模块化的/可以通过代码重用.考虑以下理想情况:

In my mind, my fragments are grouped by sections although they are still modular/reusable by code. Consider this desired scenario:

Frag1->(按Next)-> Frag2->(按Next)-> Frag3->(按Back)-> Frag1

Frag1 -> (Press Next) -> Frag2 -> (Press Next) -> Frag3 -> (Press Back) -> Frag1

经历了一系列片段之后,我想在按后退"按钮时跳过一些先前的片段(在这种情况下,跳过Frag 2).

After going through a series of fragments, I would like to skip some previous fragments (in this scenario, skip Frag 2) on pressing the back button.

但是,在我当前的代码中,我的问题是,即使返回到Frag1,Frag3也不会从屏幕上消失.发生的事情是Frag1和Frag3都在彼此的顶部可见.

However, in my current code, my problem is that even though it goes back to Frag1, Frag3 does not disappear from the screen. What happens is that both Frag1 and Frag3 becomes visible on top of each other.

以下是我相关的代码段:

Here are my relevant code snippets:

用于创建Frag1的代码段

@Override
public void onNavigationDrawerItemSelected(int position) {
    // update the main content by replacing fragments
    // init the fragment (with a default fragment, not null)
    Fragment fragment = PlaceholderFragment.newInstance(position + 1);
    // Position number from navigation sidebar starts from 0.
    // Since position starts from 0, add 1 to match section number
    // as implemented in {@link #onSectionAttached()}
    switch(position) {
        case 0:
            fragment = PlaceholderFragment.newInstance(position + 1);
            break;
        case 1: // Frag1 case
            fragment = new AddPointsFragment().newInstance(position + 1, "");
            break;
        default:
            break;
    }
    // update the main content by replacing fragments
    FragmentManager fragmentManager = getFragmentManager();
    // clear all fragments from previous section from the back stack
    fragmentManager.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
    // replace all currently added fragments in container and replace with the new fragment
    fragmentManager.beginTransaction()
            .replace(R.id.container, fragment)
            .commit();
}

用于创建Frag2的代码段

public void onEnterButtonFragmentInteraction(int sectionNumber, String cardNo) {
    // TODO: Add point for given card number
    int points = 5; //sample points
    AddPointsSuccessFragment addPointsSuccessFragment =
            new AddPointsSuccessFragment().newInstance(sectionNumber, cardNo, points);
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container, addPointsSuccessFragment)
            .addToBackStack(null)
            .commit();
}

用于创建Frag3的代码段

public void onOkButtonFragmentInteraction(int sectionNumber, String cardNo, int points) {
    RedeemRewardFragment redeemRewardFragment =
                new RedeemRewardFragment().newInstance(sectionNumber, cardNo, points);
        FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
        fragmentTransaction.replace(R.id.container, redeemRewardFragment);
        fragmentTransaction.commit();
}

我当前的解决方法是在创建Frag3并运行此代码时添加 .addToBackStack(null)

My current workaround for this is by adding .addToBackStack(null) in creating Frag3 and running this code

public void onBackButtonFragmentInteraction() {
    this.onBackPressed(); // simulate pressing of activity back button
    FragmentManager fragmentmanager = getFragmentManager();
    fragmentmanager.popBackStack(); // pop Frag2 from back stack
}

在调用onBackPressed()方法之后.不幸的是,这种解决方法很丑陋,因为在进入Frag1之前,Frag2出现了瞬间.

right after calling the onBackPressed() method. Unfortunately, this workaround is ugly because because there is a split-second appearance of Frag2 before going to Frag1.

推荐答案

所以这里解决方案的关键是这个人:

So the key to your solution here is this guy:

.addToBackStack(null)

您可以传入该特定交易的字符串标识符,而不是null,例如,我们使用的是Fragment的类名(尽管如果您在同一片段上有多个实例,则该类名将不起作用后堆栈):

Instead of null, you can pass in a String identifier for that particular transaction -- for instance, the class name of the Fragment is what we use (although that doesn't work if you have multiple instances of the same fragment on the backstack):

.addToBackStack(Fragment1.class.getName())

然后,如果要从 Fragment3 返回 Fragment1 ,只需使用下一个片段的标识符弹出,然后传递 INCLUSIVE 标志(这也将弹出您指定的下一个片段):

Then, if you wanted to get back to Fragment1 from Fragment3, just pop using the identifier of the next fragment, and pass the INCLUSIVE flag (which means it will also pop that next fragment that you specified):

getFragmentManager().popBackStack(
        Fragment2.class.getName(), 
        FragmentManager.POP_BACK_STACK_INCLUSIVE);

哪个将按预期播放动画,但是好像它们之间的片段从未出现过一样.我建议使用 next 片段的原因是因为您可能不希望将第一个片段事务放在后堆栈上.

Which will play your animations as expected, but as if the fragments in between were never there. The reason I suggest to use the next fragment is because you probably don't want your first fragment transaction on the back stack.

这篇关于在BackPressed上跳过一些片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆