Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Fragment OnAttach method

I'm new to Android development and have a question about OnAttach(Context context) method. As far as I understand you override OnAttach method in the class that you extend Fragment and what it basically does is it attaches the fragment to the Activity(Context) that gets passed as a parameter. However, in a lot of example codes I've seen on the internet, people create an interface with methods that the main Activity needs to implement and in OnAttach method, they typecast context as the interface for ex)

public class exampleFragment extends Fragment{

exampleFragmentListener activityCommander;

public interface exampleFragmentListener{
    public void someMethod(String top, String bot);

}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    try{
        activityCommander = (exampleFragmentListener)context;

    }catch(Exception e){
        throw new ClassCastException(context.toString());

    }
}

What I don't get is this piece of code :

activityCommander = (exampleFragmentListener)context;

What's the purpose of typecasting context as exampleFragmentListener? Why would we typecast our main activity to fragment? Why can't the main activity just implement the interface/implement interface methods? Thank you in advance.

like image 745
curioscurious Avatar asked Oct 28 '25 04:10

curioscurious


2 Answers

When we want a fragment to communicate with an Activity we use a interface. Now suppose if there are 2 Activities which host the same Fragment, and at runtime we don't know from which activity is the fragment is currently. That is the reason we use onAttach(). The Context provided to us in the parameter is the context of the Activity which is hosting it. So once we have the casted instance, we can use that to communicate with the activity.

like image 67
Saran Sankaran Avatar answered Oct 29 '25 18:10

Saran Sankaran


Here is an example of Fragment to Activity communication:

public class HeadlinesFragment extends ListFragment {
    OnHeadlineSelectedListener mCallback;

    // Container Activity must implement this interface
    public interface OnHeadlineSelectedListener {
        public void onArticleSelected(int position);
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (OnHeadlineSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement OnHeadlineSelectedListener");
        }
    }

    ...
}

Now the fragment can deliver messages and commuticate to the activity by calling the onArticleSelected() method (or other methods in the interface) using the mCallback instance of the OnHeadlineSelectedListener interface. So basically it's for callbacks

like image 38
Rainmaker Avatar answered Oct 29 '25 19:10

Rainmaker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!