Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a fragment in my activity dynamically?

I am using Android Studio which implements all the functions of a fragment class. I am getting following runtime exception:

must implement OnFragmentInteractionListener

Here is my code and I have implemented the OnFragmentInteractionListener in my main activity.

MAIN 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");
        }
    }
}

The remaining methods of fragment class and main activity are also implemented by default. I have also changed LinearLayout with FrameLayout in the main_activity.xmlfile and also assignedandroid:id` to it which is referenced fine. What is wrong with my code?

like image 745
user3859323 Avatar asked Jul 15 '15 08:07

user3859323


2 Answers

To interact with your BlankFragment object, I would use the following method recommended in the Android Support Docs - I believe this is what you are trying to achieve, and it will be suitable as your BlankFragment object is 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();
    }
}

In order to communicate with your fragment, you would do the following:

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

You can then call any methods associated with fragment

If it's the other way round (communication from fragment to activity), then you would 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 could explain your error. You would then use:

mCallback.onFragmentInteraction(uri);

To communicate with MainActivity

like image 177
PPartisan Avatar answered Sep 22 '22 14:09

PPartisan


This worked for me, I have just shared it here you never know it will help out some one else.

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

Just one line of code.

like image 43
Neri Avatar answered Sep 23 '22 14:09

Neri