How to close the current fragment by using Button

2019-01-21 04:51发布

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 click the button at Fragment-B , it will turn to the Fragment-C and close the Fragment-B.

If I click the back button at Fragment-C , it will back to the Fragment-A.

The code I have try is like the following

camera_album = (ImageButton) view.findViewById(R.id.camera_album);

        camera_album.setOnClickListener(new Button.OnClickListener() {
            @Override
            public void onClick(View v) {

                            closefragment();
                Fragment fragment = FileBrowserFragment.newInstance(null, null, null) ;
                MainActivity.addFragment(LocalFileBrowserFragment.this, fragment) ;


            }
        });

    private void closefragment() {
        getActivity().getFragmentManager().beginTransaction().remove(this).commit();
    }

When I click the back button at fragment-B , it turn to the Fragment-C.

But when I click the back button on Fragment-C , it doesn't back to the Fragment-A. It back to the empty background. If I want to back to Fragment-A , I have to click the back button once again.

SO , it seem doesn't close the current fragment complete.

How to finish the current fragment like the back button of Android ?

11条回答
Fickle 薄情
2楼-- · 2019-01-21 05:07
Button ok= view.findViewById(R.id.btSettingOK);
    Fragment me=this;
    ok.setOnClickListener( new View.OnClickListener(){
        public void onClick(View v){

         getActivity().getFragmentManager().beginTransaction().remove(me).commit();
        }
    });
查看更多
forever°为你锁心
3楼-- · 2019-01-21 05:12

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();
}
查看更多
叼着烟拽天下
4楼-- · 2019-01-21 05:14

From Fragment A, to go to B, replace A with B and use addToBackstack() before commit().

Now From Fragment B, to go to C, first use popBackStackImmediate(), this will bring back A. Now replace A with C, just like the first transaction.

查看更多
5楼-- · 2019-01-21 05:16

I change the code from getActivity().getFragmentManager().beginTransaction().remove(this).commit();

to

getActivity().getFragmentManager().popBackStack();

And it can close the fragment.

查看更多
▲ chillily
6楼-- · 2019-01-21 05:25

Try this:

public void removeFragment(Fragment fragment){
    android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
    android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

    fragmentTransaction.remove(fragment);
    fragmentTransaction.commit();
}
查看更多
登录 后发表回答