在Backpress上显示以前的片段除了First Fragment

时间:2017-03-04 07:30:56

标签: android fragment navigation-drawer

我正在使用导航抽屉替换片段,我想在Backpress上显示以前的片段,问题是我必须在First Fragment上显示snackbar以退出App而不是删除First Fragment并且getBackStackEntryCount()显示0计数始终。这里是我的代码

  • 碎片横断面代码

        Fragment mFragment = null;
        FragmentManager mFragmentManager = getSupportFragmentManager();
    
        switch (position){
    
            case 0://Not used Start from postion 1
                mFragment = new MainFragment().newInstance(""+position,"");
                break;
            case 1: //not used
                mFragment = MainFragment.newInstance(""+position,"");
                break;
            case 2:
                mFragment = new Download_Adr().newInstance(""+position,"");
                break;
            case 3:
                mFragment = new Adr_Status().newInstance(""+position,"");
                break;
            case 4:
                mFragment = new Update_Adr().newInstance(""+position,"");
                break;
            case 5:
                mFragment = new Update_Status().newInstance(""+position,"");
                break;
            case 6: //not useed
                mFragment = new Update_Status().newInstance(""+position,"");
                break;
            case 7: // 6th Fragment
                mFragment = new Find_Adr().newInstance(""+position,"");
                break;
            case 8:
                mFragment = new Locate_Enroll().newInstance(""+position,"");
                break;
            case 9:
                mFragment = new Locate_Enroll().newInstance(""+position,"");//same Page for Book Appointment
                break;
            case 10:
                mFragment = new FQA().newInstance(""+position,"");
                break;
            case 11:
                mFragment = new Contact_Us().newInstance(""+position,"");
                break;
    
            default:
                //mFragment = new MainFragment();
                mFragment = MainFragment.newInstance("1","");
                break;
        }
    
        if (mFragment != null)
        {
            if (position == 0 || position == 1 || position == -1)
            {
                mFragmentManager.beginTransaction().add(R.id.container, mFragment).addToBackStack("my_fragment").commit();
            }
            else
                mFragmentManager.beginTransaction().add(R.id.container, mFragment).addToBackStack("my_fragment").commit();
        }
    

onBackPressed()

 @Override
    public void onBackPressed()
    {
        if (getFragmentManager().getBackStackEntryCount() > 0)
        {
            getFragmentManager().popBackStack();
        }
        else 
        {
            super.onBackPressed();
        }
    }

3 个答案:

答案 0 :(得分:1)

首先,当您向后台堆栈添加片段时,请使用不同的名称,例如

addToBackStack("my_fragment"+position)

第二件事是当您将片段添加到后台堆栈时使用getSupportFramentManager(),但当您检查堆栈时,您正在使用getFragmentManager()

答案 1 :(得分:0)

试试这个:

首先使用backstack

将片段添加到addToBackStack()
getSupportFragmentManager().beginTransaction()
                       .add(HomeFragment, "home")
                       // Add this transaction to the back stack (name is an optional name for this back stack state, or null).
                       .addToBackStack(null)
                       .commit();

现在覆盖抽屉活动中的onBackPressed()并写下:

    @Override
public void onBackPressed(){
    FragmentManager fm = getSupportFragmentManager();
    if (fm.getBackStackEntryCount() > 0) {
        //pop fragment from backstack
        fm.popBackStack();
    } else if (fm.getBackStackEntryCount() == 0) {
            //nothing on backstack
            new AlertDialog.Builder(this)
                .setTitle("Really Exit?")
                .setMessage("Are you sure you want to exit?")
                .setNegativeButton(android.R.string.no, null)
                .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {

                    public void onClick(DialogInterface arg0, int arg1) {
                        MainActivity.super.onBackPressed();
                    }
                }).create().show();

        }
      else{
          super.onBackPressed(); 
    }
}

答案 2 :(得分:0)

when u r using snackbar right answer is   

 Fragment mFragment = null;
        FragmentManager mFragmentManager = getSupportFragmentManager();

        switch (position){

            case 0://Not used Start from postion 1
                mFragment = new MainFragment().newInstance(""+position,"");
                break;
            case 1: //not used
                mFragment = MainFragment.newInstance(""+position,"");
                break;
            case 2:
                mFragment = new Download_Adr().newInstance(""+position,"");
                break;
            case 3:
                mFragment = new Adr_Status().newInstance(""+position,"");
                break;
            case 4:
                mFragment = new Update_Adr().newInstance(""+position,"");
                break;
            case 5:
                mFragment = new Update_Status().newInstance(""+position,"");
                break;
            case 6: //not useed
                mFragment = new Update_Status().newInstance(""+position,"");
                break;
            case 7: // 6th Fragment
                mFragment = new Find_Adr().newInstance(""+position,"");
                break;
            case 8:
                mFragment = new Locate_Enroll().newInstance(""+position,"");
                break;
            case 9:
                mFragment = new Locate_Enroll().newInstance(""+position,"");//same Page for Book Appointment
                break;
            case 10:
                mFragment = new FQA().newInstance(""+position,"");
                break;
            case 11:
                mFragment = new Contact_Us().newInstance(""+position,"");
                break;

            default:
                //mFragment = new MainFragment();
                mFragment = MainFragment.newInstance("1","");
                break;
        }

        if (mFragment != null)
        {
            if (position == 0 || position == 1 || position == -1)
            {
                mFragmentManager.beginTransaction().replace(R.id.container, mFragment,"HOME")/*.addToBackStack(null)*/.commit();
            }
            else
                mFragmentManager.beginTransaction().add(R.id.container, mFragment,"my_fragment").addToBackStack("my_fragment").commit();
        }

 @Override
    public void onBackPressed()
    {


            if (getSupportFragmentManager().getBackStackEntryCount() > 0)
            {
                getSupportFragmentManager().popBackStack();
            }
            else
            {
                //super.onBackPressed();
                if (mBackPressed + TIME_INTERVAL > System.currentTimeMillis())
                {
                    finish();
                    return;
                }
                else
                {

                    Snackbar snackbar = Snackbar
                            .make(coordinatorLayout, "Press again to exit", Snackbar.LENGTH_LONG)
                            .setAction("Exit", new View.OnClickListener() {
                                @Override
                                public void onClick(View view)
                                {
                                    finish();
                                }
                            });


                    snackbar.setActionTextColor(Color.RED);


                    View sbView = snackbar.getView();
                    TextView textView = (TextView) sbView.findViewById(android.support.design.R.id.snackbar_text);
                    textView.setTextColor(Color.YELLOW);
                    snackbar.show();
                    //Toast.makeText(getBaseContext(), "Tap back button in order to exit", Toast.LENGTH_SHORT).show();
                }

                mBackPressed = System.currentTimeMillis();
            }
    }