Search code examples
javaandroidinheritancemultiple-inheritancechromecast

Substitution for extending multiple classes in Java


As I'm adding a cast button which only supports classes that extend the ActivityFragment class, I found it difficult since the specific class I'm interested in already extends another LinearLayout class. To avoid creating a custom button, I did the following workaround,

public class metaView extends LinearLayout {
    private FragmentActivity fragmentActivity;
    private void initViews() {
        fragmentActivity = new FragmentActivity(){
            @Override
            protected void onCreate(Bundle savedInstanceState){
                    //Create Button Discovery...

            }
        };
    }
}

However, it does not seem like the overriden method onCreate ever gets called. Does anyone see the problem with this alternative? or would this just not work?

Thanks in advance!


Solution

  • As I'm adding a cast button which only supports classes that extend the ActivityFragment class

    That doesn't make any sense. Regardless, you cannot create and use an anonymous Activity instance. Your layout should be configurable to show/hide the button, and provide an interface to handle the click on the button. For example your MetaView class could have something like this:

    public interface OnCastButtonClickedListener {
        void onCastButtonClicked();
    }
    
    public void setCastButtonEnabled(boolean enabled) {
        // Turn it on or off
    }
    
    public void setOnCastButtonClickedListener(OnCastButtonClickedListener l) {
        // Assign some listener to delegate the button click to.
    }
    

    And then, whatever Activity is using the layout becomes responsible for showing/hiding the cast button, and handling the click event.