How to implement OnFragmentInteractionListener

后端 未结 12 2505
花落未央
花落未央 2020-11-22 06:25

I have a wizard generated app with navigation drawer in android studio 0.8.2

I have created a fragment and added it with newInstance() and I get this error:

相关标签:
12条回答
  • 2020-11-22 07:03

    You should try removing the following code from your fragments

        try {
            mListener = (OnFragmentInteractionListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    

    The interface/listener is a default created so that your activity and fragments can communicate easier

    0 讨论(0)
  • 2020-11-22 07:03

    I'd like to add the destruction of the listener when the fragment is detached from the activity or destroyed.

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }
    

    and when using the new onStart() method with Context

    @Override
    public void onDestroy() {
        super.onDestroy();
        mListener = null;
    }
    
    0 讨论(0)
  • 2020-11-22 07:09

    For those of you who visit this page looking for further clarification on this error, in my case the activity making the call to the fragment needed to have 2 implements in this case, like this:

    public class MyActivity extends Activity implements 
        MyFragment.OnFragmentInteractionListener, 
        NavigationDrawerFragment.NaviationDrawerCallbacks {
        ...// rest of the code
    }
    
    0 讨论(0)
  • 2020-11-22 07:12

    For those of you who still don't understand after reading @meda answer, here is my concise and complete explanation for this issue:

    Let's say you have 2 Fragments, Fragment_A and Fragment_B which are auto-generated from the app. On the bottom part of your generated fragments, you're going to find this code:

    public class Fragment_A extends Fragment {
    
        //rest of the code is omitted
    
        public interface OnFragmentInteractionListener {
            // TODO: Update argument type and name
            public void onFragmentInteraction(Uri uri);
        }
    }
    
    public class Fragment_B extends Fragment {
    
        //rest of the code is omitted
    
        public interface OnFragmentInteractionListener {
            // TODO: Update argument type and name
            public void onFragmentInteraction(Uri uri);
        }
    }
    

    To overcome the issue, you have to add onFragmentInteraction method into your activity, which in my case is named MainActivity2. After that, you need to implements all fragments in the MainActivity like this:

    public class MainActivity2 extends ActionBarActivity
            implements Fragment_A.OnFragmentInteractionListener, 
                       Fragment_B.OnFragmentInteractionListener, 
                       NavigationDrawerFragment.NavigationDrawerCallbacks {
        //rest code is omitted
    
        @Override
        public void onFragmentInteraction(Uri uri){
            //you can leave it empty
        }
    }
    

    P.S.: In short, this method could be used for communicating between fragments. For those of you who want to know more about this method, please refer to this link.

    0 讨论(0)
  • 2020-11-22 07:13

    In addition to @user26409021 's answer, If you have added a ItemFragment, The message in the ItemFragment is;

    Activities containing this fragment MUST implement the {@link OnListFragmentInteractionListener} interface.
    

    And You should add in your activity;

    public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener, ItemFragment.OnListFragmentInteractionListener {
    
    //the code is omitted
    
     public void onListFragmentInteraction(DummyContent.DummyItem uri){
        //you can leave it empty
    }
    

    Here the dummy item is what you have on the bottom of your ItemFragment

    0 讨论(0)
  • 2020-11-22 07:18

    Just an addendum:

    OnFragmentInteractionListener handle communication between Activity and Fragment using an interface (OnFragmentInteractionListener) and is created by default by Android Studio, but if you dont need to communicate with your activity, you can just get ride of it.

    The goal is that you can attach your fragment to multiple activities and still reuse the same communication approach (Every activity could have its own OnFragmentInteractionListener for each fragment).

    But and if im sure my fragment will be attached to only one type of activity and i want to communicate with that activity?

    Then, if you dont want to use OnFragmentInteractionListener because of its verbosity, you can access your activity methods using:

    ((MyActivityClass) getActivity()).someMethod()
    
    0 讨论(0)
提交回复
热议问题