How can I extend and implement two libraries at the same time?

时间:2016-03-04 18:15:05

标签: android

I would like to build an activity that uses the Youtube API, and App compact activity.

How can I merge these two libraries together:

Youtube library:

public class Activity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener { }

App compact activity library

public class Activity extends AppCompatActivity { }

Thank you.

1 个答案:

答案 0 :(得分:2)

Multiple inheritence is not supported by Java (for better or for worse), and YouTubeBaseActivity extends Activity, if YoutubeBaseActivity extended AppCompatActivity you would have had your wish !

In my opinion not supporting multiple inheritence was the right way as it can cause the diamond problem of multiple inheritance among inexperienced developers. To Quote WikiPidea

The "diamond problem" (sometimes referred to as the "deadly diamond of death") is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and C have overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?

As for your problem

I would like to create an activity that shows an youtube video at the top, and a pager adapter at the bottom. I need to call a method called "getSupportFragmentManager()", but I can't do it without extending the AppCompatActivity, thats why I was trying to use both

Use YouTubePlayerSupportFragment if you are only playing a YouTube video in a single support.v4.app.Fragment. This allows you to use FragmentActivity, rather than YouTubeBaseActivity.