Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Casting Interface in Activity

Tags:

java

android

The matter is that, before this time, I implemented interfaces in Activity, values ​​of methods set in Fragment. That is, in onAttach() initialized, and then where to call the interface method called. Example:

interface OnNumberChangeCallback{
    void onSuccess();
}

In Fragment:

OnNumberChangeCallback onNumberChangeCallback;

onAttach(Context context){
    onNumberChangeCallback = (OnNumberChangeCallback) context;
}

etc.

I want to initialize the interface in BaseActivity. And in Fragment to implement this interface. When I try to initialize in onCreate method this way:

onNumberChangeCallback = (OnNumberChangeCallback) this;

I get the error:

BaseActivity cannot be cast to OnNumberChangeCallback

Question: How to properly initialize the interface in Activity?

like image 520
No Name Avatar asked Jun 16 '26 21:06

No Name


1 Answers

It sounds like you want the Fragment to implement the interface, and have the Activity store a reference to it somehow.

Your interface:

interface OnNumberChangeCallback {
    void onSuccess();
}

To implement it in your fragment:

public class MyFragment extends Fragment implements OnNumberChangeCallback {

    @Override
    public void onSuccess() {
        // TODO
    }

    // other fragment methods, like onCreateView() etc
}

And to save the listener in your activity:

public class MyActivity extends AppCompatActivity {

    private OnNumberChangeCallback callback;

    @Override
    public void onAttachFragment(Fragment fragment) {
        super.onAttachFragment(fragment);

        if (fragment instanceof OnNumberChangeCallback) {
            callback = (OnNumberChangeCallback) fragment;
        }
    }

    // other activity methods, like onCreate() etc
}

Then, to use the callback, you could write something like this anywhere inside your activity:

public void onButtonClick() {
    if (callback != null) {
        callback.onSuccess();
    }
}

The root of this solution is that FragmentActivity (which is a superclass of AppCompatActivity) has a method onAttachFragment() that is called any time a Fragment is attached to your activity. In the same way you could cast your Context to an interface inside a fragment by using onAttach(), you can cast a Fragment to an interface using onAttachFragment().

Documentation: https://developer.android.com/reference/android/support/v4/app/FragmentActivity.html#onAttachFragment(android.support.v4.app.Fragment)

PS: This answer provides a very generic solution, and one that should work for almost any way you're adding Fragments to your Activity. If, however, you know that Fragments will be added in a particular way, you could probably do something much simpler than implementing onAttachFragment()... you could just cast the Fragment as you add it to your activity, like this:

    MyFragment fragment = new MyFragment();
    callback = fragment;

    getSupportFragmentManager()
            .beginTransaction()
            .add(R.id.content, fragment, "fragmentTag")
            .commit();
like image 130
Ben P. Avatar answered Jun 19 '26 10:06

Ben P.



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!