如何摧毁我想要的碎片 [英] How to destroy fragments that i want

查看:152
本文介绍了如何摧毁我想要的碎片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题有点难以解释。我有很多片段,我会称它们为F1,F2,F3 ...
$ b MainActivity 加载F1 (片段1)。有一个按钮。当用户点击时,用F2代替。在F2中也有一个按钮。点击后替换为F3。好吧,现在看到这里。在F3中有一个名为languages的按钮,右边是6个imageViews。

所以当用户单击语言按钮时,F3会替换为F4。在FD中,我有一个带有自定义适配器的列表视图( checkbox imageView textView )。当用户选择复选框,然后点击下一步,F4取代F3,但实际上这是一个新的片段(F5)。



在MainActivity中,我编写了这段代码。

  @Override 
public void onBackPressed(){
if(getFragmentManager()。getBackStackEntryCount()== 1) {
finish();
} else if(getFragmentManager()。getBackStackEntryCount()> = 5){
getFragmentManager()。popBackStack(fragmentLanguage.getClass()。getName(),FragmentManager.POP_BACK_STACK_INCLUSIVE);
} else {
super.onBackPressed();


$ / code $ / pre

所有工作都很好,在F5中,当我点击它时回到F3。这就是我需要的。但我有一个问题。当我再次单击F5语言按钮时,这意味着F5替换为F6,而当在F6中时,我单击下一个,然后F6替换为F7。在F7中,当我点击时,它会回到F5,然后如果我再点击一次,它将会返回到F3。当用户点击F7,F9或F11时,我需要点击返回F3。现在,如果我从F11单击回到F9,然后F7,然后F5然后F3。我怎样才能做到这一点?



fragmentLanguage是F4(或F6或F8)。 F3或F5或F7被命名为personalInfoFragment。



在我的代码中,您会看到OnBackPressed方法是?现在看,它会得到backStack中有多少片段,并且如果在backStack 5个或更多片段中,它将删除F4(语言片段是F4)。没关系,我的问题是,如果我打开F6片段,我需要删除F5片段。但F5是相同的F3,所以他们有相同的标签,如果我说删除带有该标签的片段,F3也会删除。这是我的问题。如果我打开F6我需要删除F5,但F3和F5具有相同的标记。



对不起,不好解释。我不能很好地解释我的问题。
我需要,在点击返回到FC从所有碎片(FD,FE,FG ...)。

解决方案 div>

此方法删除所有片段:

  / ** 
*删除Fragment中的所有片段BackStack
* /
private void removeAllFragments(){
FragmentManager fm = getSupportFragmentManager();
for(int i = 0; i< fm.getBackStackEntryCount(); ++ i){
fm.popBackStack();


$ / code $ / pre

要删除您选择的片段,通过传递他们独特的TAG,

这将通过使用TAG找到片段:

  Fragment fragment = getSupportFragmentManager()。findFragmentByTag(TAG_FRAGMENT); 
if(fragment!= null)
getSupportFragmentManager()。beginTransaction()。remove(fragment).commit();

这会通过传递TAG来推动片段:

  getSupportFragmentManager()。beginTransaction()
.replace(R.id.fragment_container_fl,new BookRideFragment(getApplicationContext())
,FRAGMENT_BOOK_RIDE)
。承诺();

希望这有助于解释您的问题并不清楚。

My question a little bit hard to explain. I have many fragments, I'll call them F1, F2, F3...

In MainActivity loads F1 (Fragment 1). There is a button. When the user clicks, that replaces with F2. In F2 there's a button too. After clicking replaces with F3. Ok now see here. In F3 there is a button named languages and right of him 6 imageViews.

So when user click languages button, F3 replaces with F4. In FD i have a listView with custom adapter (checkbox, imageView, textView). When users selects checkboxes, and clicks next, F4 replaces with F3, but in real that's a new fragment (F5).

In MainActivity i wrote this code.

    @Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() == 1) {
        finish();
    } else if (getFragmentManager().getBackStackEntryCount() >= 5) {
        getFragmentManager().popBackStack(fragmentLanguage.getClass().getName(), FragmentManager.POP_BACK_STACK_INCLUSIVE);
    } else {
        super.onBackPressed();
    }
}

All works good, in F5 when i click back it get's back to F3. That's all i need. But I have one question. When i click in F5 languages button one more time, that means that F5 replaces with F6, and when in F6 I click next, then F6 replaces with F7. In F7 when i click back, it get's back to F5, then if I click back one more time that's will get to F3. I need, when user clicks back on F7, or in F9 or in F11, then after clicking back it got back to F3. Right now if i click back from F11, it will get to F9, then F7, then F5 and then F3. How can i do this part?

fragmentLanguage is the F4 (or F6 or F8). And F3 or F5 or F7 is named as personalInfoFragment.

In my code you see the OnBackPressed method yes? Now watch, it gets how many fragments is in backStack, and if in backStack 5 or more fragments it removes F4 (language fragment is the F4). That's okay, my question is that if i open F6 fragment, I need to delete F5 fragment . But F5 is the same F3, so they have the same tag, if i say remove fragment with that tag, F3 removes too. That's my problem. If i open F6 i need to remove F5, but F3 and F5 has the same tag.

Sorry for bad explaining. I can't to explain my problem well. I NEED, THAT ON BACK CLICK THAT GOT BACK TO FC FROM ALL FRAGMENTS (FD, FE, FG...).

解决方案

This method removes all fragments:

    /**
     * Remove all fragments in Fragment BackStack
     */
    private void removeAllFragments() {
        FragmentManager fm = getSupportFragmentManager();
        for (int i = 0; i < fm.getBackStackEntryCount(); ++i) {
            fm.popBackStack();
        }
    }

For removing fragment of your choice, you have push fragment by passing their unique TAG,

This will find the fragment by using TAG:

Fragment fragment = getSupportFragmentManager().findFragmentByTag(TAG_FRAGMENT);
if(fragment != null)
    getSupportFragmentManager().beginTransaction().remove(fragment).commit();

This will push fragment by also passing TAG:

getSupportFragmentManager().beginTransaction()
                        .replace(R.id.fragment_container_fl, new BookRideFragment(getApplicationContext())
                                , FRAGMENT_BOOK_RIDE)
                        .commit();

Hope it helps as the explanation of your question is not clear.

这篇关于如何摧毁我想要的碎片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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