Android - can't open fragment a second time

I open fragment

and shortly after it closes.

It opens and doesn't run any problems the first time, but the second time when he tries to open it, it appears NullPointerException

.

To open fragment

with a class SomeFrag

that extends fragment

. To close it, I call the method closeFrag()

.

public class SomeFrag extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        mRootview = (ViewGroup) inflater.inflate(R.layout.somefrag, container, false); // line with error
        return mRootview;
    }

    public void closeFrag(){
        ((ViewGroup)mRootview.getParent()).removeView(mRootview);
    }
}

      

Why can't I open the fragment a second time?

+3


source to share


1 answer


To delete a fragment, you must use a transaction like



getSupportFragmentManager().beginTransaction().remove(fragmentInstance).commit();

      

+1


source







All Articles