How to close the current fragment by using Button like the back button?

前端 未结 13 872
失恋的感觉
失恋的感觉 2020-12-07 14:02

I have try to close the current fragment by using Imagebutton.

I am in Fragment-A and it will turn to the Fragment-B when I click the button.

And when I clic

相关标签:
13条回答
  • 2020-12-07 14:59

    You can try this logic because it is worked for me.

    frag_profile profile_fragment = new frag_profile();
    
    boolean flag = false;
    @SuppressLint("ResourceType")
    public void profile_Frag(){
        if (flag == false) {
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            manager.getBackStackEntryCount();
            transaction.setCustomAnimations(R.anim.transition_anim0, R.anim.transition_anim1);
            transaction.replace(R.id.parentPanel, profile_fragment, "FirstFragment");
            transaction.commit();
            flag = true;
        }
    
    }
    
    @Override
    public void onBackPressed() {
        if (flag == true) {
            FragmentManager manager = getFragmentManager();
            FragmentTransaction transaction = manager.beginTransaction();
            manager.getBackStackEntryCount();
            transaction.remove(profile_fragment);
            transaction.commit();
            flag = false;
        }
        else super.onBackPressed();
    }
    
    0 讨论(0)
提交回复
热议问题