MainActivity to fragment and back to MainActivity on pressing back button

前端 未结 4 1283
说谎
说谎 2020-12-20 00:26

I have a Mainactivity which contains a Layout which is parent of 4 sub layout. on clicking on sub layout i am going to a new fragment replacing main layout. But i cant go ba

相关标签:
4条回答
  • 2020-12-20 01:08

    Here is how it can be done in Xamarin:

    Load fragment (I wrote helper method for that, but it's not necessary):

    public void LoadFragment(Activity activity, Fragment fragment)
    {
        var backStateName = fragment.GetType().Name;
        var fm = activity.FragmentManager;
        var ft = fm.BeginTransaction();
    
        ft.Replace(Resource.Id.mainContainer, fragment);
        ft.AddToBackStack(backStateName);
    
        ft.Commit();
    }
    

    Back button (in MainActivity):

    public override void OnBackPressed()
    {
        if (isNavDrawerOpen()) drawerLayout.CloseDrawers();
        else
        {
            var backStackEntryCount = FragmentManager.BackStackEntryCount;
    
            if (backStackEntryCount == 1) Finish();
            else if (backStackEntryCount > 1) FragmentManager.PopBackStack();
            else base.OnBackPressed();
        }
    }
    

    isNavDrawerOpen method:

    bool isNavDrawerOpen()
    {
        return drawerLayout != null && drawerLayout.IsDrawerOpen(Android.Support.V4.View.GravityCompat.Start);
    }
    
    0 讨论(0)
  • 2020-12-20 01:17

    If you have one Activity and four fragments then set onBackPressed() as below in your MainActivity.

    @Override
            public void onBackPressed()
            {
                super.onBackPressed();
                finish();          
            }
    

    And in fragments:

     @Override
        public void onResume() {
    
            super.onResume();
            new PlayListFragment();
            getView().setFocusableInTouchMode(true);
            getView().requestFocus();
            getView().setOnKeyListener(new View.OnKeyListener() {
                @Override
                public boolean onKey(View v, int keyCode, KeyEvent event) {
    
                    if (event.getAction() == KeyEvent.ACTION_UP && keyCode == KeyEvent.KEYCODE_BACK){
                        if(getFragmentManager().getBackStackEntryCount() > 0) {
                          getFragmentManager().popBackStack();
                        }
    
                        return true;
    
                    }
    
                    return false;
                }
            });
        }
    
    0 讨论(0)
  • 2020-12-20 01:23

    You can Override the onBackPressed of MainActivity

    @Override
    public void onBackPressed() {
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
    
    0 讨论(0)
  • 2020-12-20 01:25

    Try Like this

        public boolean popFragment() {
        boolean isPop = false;
    
        Fragment currentFragment = getSupportFragmentManager()
                .findFragmentById(R.id.flContent);
    
            if (getSupportFragmentManager().getBackStackEntryCount() > 0) {
                isPop = true;
                getSupportFragmentManager().popBackStackImmediate();
            }
    
        return isPop;
    }
    
    @Override
    public void onBackPressed() {
        if (!popFragment()) {
            finish();
        }
    }
    
    public void replaceFragment(Fragment fragment, boolean addToBackStack) {
    
        FragmentTransaction transaction = getSupportFragmentManager()
                .beginTransaction();
    
        if (addToBackStack) {
            transaction.addToBackStack(null);
    
        } else {
            getSupportFragmentManager().popBackStack(null,
                    FragmentManager.POP_BACK_STACK_INCLUSIVE);
    
        }
        transaction.replace(R.id.fragment_container, fragment);
        transaction.commit();
        getSupportFragmentManager().executePendingTransactions();
    
    }
    

    add above method is in your parent Activity And Use like

     FragmentAboutUs fragmentAboutUs = new FragmentAboutUs();
     replaceFragment(fragmentAboutUs , true);
    
    0 讨论(0)
提交回复
热议问题