How to add a fragment to your activity dynamically?

I use Android Studio, which implements all the functions of the fragment class. I get the following exception at runtime:

must implement OnFragmentInteractionListener 

Here is my code, and I implemented OnFragmentInteractionListener in my main action.

PRIMARY ACTIVITY:

 public class MainActivity extends FragmentActivity implements BlankFragment.OnFragmentInteractionListener { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (findViewById(R.id.fragment_container) != null) { BlankFragment fragment = new BlankFragment(); android.support.v4.app.FragmentManager manager = getSupportFragmentManager(); android.support.v4.app.FragmentTransaction ft = manager.beginTransaction(); ft.add(R.id.fragment_container, fragment).commit(); } @Override public void onFragmentInteraction(Uri uri) {} } 

BLANK FRAGMENT:

 public class BlankFragment extends android.support.v4.app.Fragment { private OnFragmentInteractionListener mListener; public interface OnFragmentInteractionListener { public void onFragmentInteraction(Uri uri); } public void onAttach(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } } 

Other methods of the fragment class and main activity are also implemented by default. I also changed LinearLayout to FrameLayout in main_activity.xml file and also assigned android: id`, which is fined. What is wrong with my code?

+5
source share
2 answers

To interact with your BlankFragment object, I would use the following method recommended in Android Support Docs - I believe that this is what you are trying to achieve, and it will be suitable since your BlankFragment object BlankFragment hosted by MainActivity :

 public class MainActivity extends FragmentActivity implements BlankFragment.OnFragmentInteractionListener { private BlankFragment fragment = null; private android.support.v4.app.FragmentManager manager = null; private android.support.v4.app.FragmentTransaction ft; public void onFragmentInteraction(Uri uri){ Log.i("Tag", "onFragmentInteraction called"); } protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (manager == null) manager = getSupportFragmentManager(); if(manager.findFragmentById(R.id.fragment_container) == null) { //If a fragment is not already loaded into your container, then add one... fragment = new BlankFragment(); ft= manager.beginTransaction(); ft.add(R.id.fragment_container,fragment).commit(); } } 

To contact your snippet, you must do the following:

 if(fragment == null) { fragment = (BlankFragment) manager.findFragmentById(R.id.fragment_container); } 

Then you can call any methods related to fragment

If it's the other way around (linking from fragment to activity), you will need to do the following in BlankFragment to form a link with the parent Activity :

 //Class variable... OnFragmentInteractionListener mCallback; @Override public void onAttach(Activity activity) { super.onAttach(activity); try { mCallback = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } 

You may have forgotten about this last step, which may explain your mistake. Then you would use:

 mCallback.onFragmentInteraction(uri); 

To contact MainActivity

+1
source

It worked for me, I just shared it here, you never know that it will help anyone else.

 getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit(); 

Only one line of code.

0
source

All Articles