简体   繁体   English

片段和backStack显示/隐藏

[英]Fragments and the backStack show/hide

First of all thankyou for looking at this. 首先感谢您的关注。 I have 4 Fragments. 我有4个片段。 One is a Menu Fragment that displays the menu. 一个是显示菜单的菜单片段。 I have 2 List Fragments that display a list of data and pictures. 我有2个显示数据和图片列表的列表片段。 the 4th fragment is a details fragment. 第四个片段是详细信息片段。 Everything starts out normal. 一切开始正常。

Setup: 设定:

I click a list button it shows up and the menu frag is hidden behind it. 我单击显示的列表按钮,并且菜单碎片隐藏在其后面。

I click on an entry in the list fragment it uses an interface to pass data and show the details fragment. 我单击列表片段中的一个条目,该条目使用一个接口来传递数据并显示详细信息片段。

I click the back button once it takes me to the list fragment like it should. 单击后退按钮后,它会带我到应有的列表片段。

I click the back button again and it shows the menu frag like it should. 我再次单击“后退”按钮,它显示菜单碎片,如其应有的那样。

Problem: 问题:

If i click on a list button again the list shows up but the menu fragment is still showing behind it. 如果我再次单击列表按钮,则列表会显示出来,但菜单片段仍显示在其后面。

I am assuming that it has something to do with the backstack and maybe i am not using it correctly. 我假设它与backstack有关,也许我没有正确使用它。 Any help is greatly appreciated. 任何帮助是极大的赞赏。 If someone could explain this issue that would be amazing. 如果有人可以解释这个问题,那将是惊人的。 If you would like me to post any part of my conde just let me know but i feel like my issue is with the backstack and this is the part of my code that i mess with the backstack. 如果您希望我发布conde的任何部分,请告诉我,但是我觉得我的问题出在backstack上,这是我在backstack中弄乱的代码的一部分。

private Fragment visible;
private Fragment mVisibleCached;
@Override
public void onBackPressed() {
    super.onBackPressed();
    visible = mVisibleCached;

}

. .

public void showFragment(int fragIn) {
    final FragmentManager fm = getSupportFragmentManager();
    final FragmentTransaction ft = fm.beginTransaction();
    ft.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
    if (visible != null) {
        ft.hide(visible);
        //ft.remove(visible);
        mVisibleCached = visible;
        ft.addToBackStack(null);

    }
    switch (fragIn) {
    case 0:
        menuFrag = ((MenuFragment) fm.findFragmentByTag(MenuFragment.TAG));
        if (menuFrag == null) {
            menuFrag = MenuFragment.newInstance();
            ft.add(R.id.fragment_container, menuFrag, MenuFragment.TAG);
        } else {
            // ft.show(menuFrag);
            //ft.add(R.id.fragment_container, menuFrag, MenuFragment.TAG);
            if (!menuFrag.isVisible()) {
                ft.show(menuFrag);
            }
        }

        ft.addToBackStack(null);
        visible = menuFrag;
        break;
    case 1:
        programsFrag = ((ListFrag) fm.findFragmentByTag(PROGRAMS_TAG));
        if (programsFrag == null) {
            programsFrag = ListFrag.newInstance(false);
            ft.add(R.id.fragment_container, programsFrag, PROGRAMS_TAG);
        } else {
            // ft.show(programsFrag);
            //ft.add(R.id.fragment_container, programsFrag, PROGRAMS_TAG);

            // ft.replace(R.id.fragment_container, programsFrag);
            if (!programsFrag.isVisible()) {
                ft.show(programsFrag);
            }
        }
        ft.addToBackStack(null);
        visible = programsFrag;
        break;
    case 2:
        hostsFrag = ((ListFrag) fm.findFragmentByTag(HOSTS_TAG));
        if (hostsFrag == null) {
            hostsFrag = ListFrag.newInstance(true);
            ft.add(R.id.fragment_container, hostsFrag, HOSTS_TAG);
        } else {
            // ft.show(hostsFrag);
            //ft.add(R.id.fragment_container, hostsFrag, HOSTS_TAG);

            // ft.replace(R.id.fragment_container, hostsFrag);
            if (!hostsFrag.isVisible()) {
                ft.show(hostsFrag);
            }
        }
        ft.addToBackStack(null);
        visible = hostsFrag;
        break;
    case 3:
        detailsFrag = ((DetailsFrag) fm.findFragmentByTag(DetailsFrag.TAG));
        if (detailsFrag == null) {
            detailsFrag = DetailsFrag.newInstance();
            ft.add(R.id.fragment_container, detailsFrag, DetailsFrag.TAG);
        } else {
            //ft.add(R.id.fragment_container, detailsFrag, DetailsFrag.TAG);
            if (!detailsFrag.isVisible()) {
                ft.show(detailsFrag);
            }
        }
        ft.addToBackStack(null);
        visible = detailsFrag;
        break;
    }
    ft.commit();
}

I solved the issue by following your method, it is not elegant but it works for me.. 我通过遵循您的方法解决了这个问题,虽然它并不优雅,但对我有用。

Instead of your 2 fragments I used an integer var and an arraylist of integer (the integer are ids of fragments) 而不是您的2个片段,我使用了一个整数var和一个整数的数组列表(整数是片段的ID)

int visible;

ArrayList<Integer> cachedVisible= new ArrayList<Integer>();

Then in the method showing the right fragment i did something like this 然后在显示正确片段的方法中,我做了这样的事情

private void setVisibleFragment(int id) {
if(visible!=0){
        cachedVisible.add(visible);
    }else{
        cachedVisible.add(R.id.home);
    }


   switch (id) {
        case R.id.settings:
        visible=R.id.settings;
        break;
    case R.id.services:
        ft.hide(fm.findFragmentById(R.id.settings_frag));
        ft.hide(fm.findFragmentById(R.id.plugin_frag));
        ft.show(fm.findFragmentById(R.id.service_frag));
        ft.addToBackStack(null);
        ft.commitAllowingStateLoss();
        visible=R.id.services;
        break;
        ....
   }
}

And at the end in the FragmentActivity 最后是FragmentActivity

public void onBackPressed() {
    if(cachedVisible.size()==0){
        finish();
    }else{
    visible = cachedVisible.get(cachedVisible.size()-1);
    cachedVisible.remove(cachedVisible.size()-1);
     }
    setVisibleFragment(visible);
    cachedVisible.remove(cachedVisible.size()-1);   
}

Hope it can help someone ;) 希望它可以帮助某人;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM