Reference to elements of fragment from activity

后端 未结 1 1288
心在旅途
心在旅途 2021-01-26 08:14

It is possible to get reference to element(like button) defined in xml layout of fragment and use it in another activity?

I tried to do that but have null object referen

相关标签:
1条回答
  • 2021-01-26 08:34

    For removing a Fragment, via a button press inside said Fragment.

    Method One (Potentially the simplest)

    I haven't tested this, so it may not be possible...but you could try and access the FragmentManager from inside this fragment, and then have it remove itself. In this case, you would call this inside your onClick(). You may need to place getActivity() in front of getFragmentManager() here.

    getFragmentManager().beginTransaction().remove(this).commit();
    

    Method Two (Almost as simple, but bad practice)

    Place the above logic inside a public method in the Activity class your Fragment is attached to, and access it inside your Fragment onClick() like so:

    ((MyActivityName)getActivity()).nameOfPublicMethodToRemoveFragment();
    

    Method Three (Recommended way for a Fragment to Communicate with its Activity)

    Use an interface (Example pulled from here):

        public class BlankFragment extends Fragment implements View.OnClickListener{
    
        private View rootView;
        private Button button;
    
        private OnFragmentInteractionListener mListener;
    
        public static BlankFragment newInstance() {
            return new BlankFragment();
        }
    
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mListener = (OnFragmentInteractionListener) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException(activity.toString()
                        + " must implement OnFragmentInteractionListener");
            }
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
    
            rootView = inflater.inflate(R.layout.fragment_blank, container, false);
            button = (Button) rootView.findViewById(R.id.fragment_button);
            button.setOnClickListener(this);
    
            return rootView;
        }
    
        @Override
        public void onDetach() {
            super.onDetach();
            mListener = null;
        }
    
        @Override
        public void onClick(View v) {
            mListener.onFragmentInteraction();
        }
    
        public interface OnFragmentInteractionListener {
            void onFragmentInteraction();
        }
    
    }
    

    Main Activity

    public class MainActivity extends AppCompatActivity implements BlankFragment.OnFragmentInteractionListener{
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            if (getFragmentManager().findFragmentById(R.id.fragment_container) == null) {
                getFragmentManager()
                        .beginTransaction()
                        .add(R.id.fragment_container, BlankFragment.newInstance())
                        .commit();
            }
        }
    
        @Override
        public void onFragmentInteraction() {
            //Remove Fragment Here
        }
    }
    

    Method Four (Alternative)

    Use an EventBus to communicate from the Fragment to the Activity

    0 讨论(0)
提交回复
热议问题