How can I extend and implement two libraries at the same time? - 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.

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.

Related

How to extend ActionBarActivity and YoutubeBaseActivity?

Is there a way I can extend both of these in a single activity? If yes, please share with me the source code.
From another SO answer:
To reduce the complexity and simplify the language, Android does not support multiple inheritance as it's based on Java programming language. Hence you can't extend both ActionBarActivity and YoutubeBaseActivity in a Single Activity.
The solution is pretty simple: use the YouTubePlayerFragment class. This does not pose any requirement on the Activity, leaving you with plenty of options for your theming.
Since the version 22.1.0, the class ActionBarActivity is deprecated. You should use AppCompatActivity.
Note : ActionBarActivity is deprecated, use AppCompatActivity.
Instead of having the Youtube player in the Activity (extending YoutubeBaseActivity), make your Activity extends from AppCompatActivity and use a YoutubePlayerFragment inside the AppCompatActivity. You will be able to use all the features of AppCompat with your Youtube video.
If you REALLY want to use BaseYoutubeActivity, you have to add an AppCompatDelegate in your own Activity extending the BaseYoutubeActivity and use it in every lifecycle method of your activity. Read the documentation of the delegate and read the original source code of AppCompatActivity to understand the delegate.
You cannot do that for now. you can use fragment instead of activity in your Activity that extends AppCompatActivity
Please refer to this answer. https://stackoverflow.com/a/30101931/4321808

Adding functionality to the activity (inheritance, decoration, strategy ... )

I cannot decide what approach should I use in the next situtation.
One activity from my app need to have different functionality, here is the leak of multiple inheritence comes into play.
What I need to get ?
There are several parts that my activity has to have.
Navigation drawer. I am using MaterialDrawer library. It requires activity to implement on click callbacks (or use composition instead), but also it use activity as constructor argument,so I think it will be better to put this into separate class inherited from Activity or any base class provided by Android Framework . Thanks to library developer it doesn't require any stuff to be done in on create method ( setContentLayout for instance)
My activity(one of several) will have only Toolbar and FrameLayout for holding fragment . Or it is better to separate toolbar and single fragment ?
This in turn requires some stuff to be done in onCreate : setContentLayout with basic layout , add fragment to the container set activity actionbar .....
Maybe in future I will use another libraries that requires to add something in activity lifecycle methods.
All these points in order to follow Single Responsibility principle have to be separate classes inherited from some base Activity.
For example we will have something like this.
public class SingleFragmentActivity<T extends Fragment> extends AppCompatActivity {
}
public class SingleFragmentToolbarActivity<T extends Fragment> extends AppCompatActivity {
}
public class NavigationDrawerActivity extends AppCompatActivity {
}
....
As you can see each functionality is put into the separate class, but what if I need to have SingleFramgentActivity with NavigationDrawer ?
In this case I have to inherit one of these classes from another, for example
public class NavigationDrawer extends SingleFragmentActivity {
}
If to do so, I have no ability to use navigation drawer separately from SingleFragmentActivity.
What is the best practice to follow in this case, how to build class hierarchy to make it flexible and use Open-Close principle, to make changes in application without any pain. (Use strategy, decorator ..... ?)
I would be grateful everyone for help.

How to call fragment from activity without using fragmentactivity?

In my project i am using fragments and activities. in one of activity i used youtube video api, now i want to call fragment from that activity but they are not allowing without fragmentactivity and i extended youtubebaseactivity in that activity.so please help me
extend yourclass with ActionBarActivity and you can Access both functionality.
Thats it...

Capture Android App Launch and App Minimize events

I have to do capture Applaunch and App minimize events for myapp for Appirater integration. I have to show Appirater dialog for every 3rd app launch/app minimize
I followed the article
http://vardhan-justlikethat.blogspot.in/2013/05/android-solution-to-detect-when-android.html
But i have a problem, i have many activities,fragments in Application.say some Activities extends FragmentActivity, some of the them are fragments and some of them extends Activity
But, as per the article, i have to extend all the activities using Base Activity
It implies the architecture changes for my application.
If i write the code separate for each activities, the Appirater wonk work proper. Any alternatives to this ? please help in fixing
Tried also by extending Application class but it can capture app minimze events
Create a BaseActivity And a BaseFragmentActivity that does the same as the BaseActivity in the example, and then extend them in the appropriate places, should still work the same and you don't have to do much work. Correct me if I'm wrong, I just skimmed through it
Edit, example:
public class BaseActivity extends Activity{
//everything
}
and the other
public class BaseFragmentActivity extends FragmentActivity{
//everything
}

Extending FragmentActivity instead of Activity

I have my application code base with multiple activities in it. I have a BaseActivity class which extends Activity class and is the parent class for all Activities in my application. BaseActivity takes care of the state my application goes in background and comes back to foreground.
Now I have written few more Activities which are using fragments so these new Activities are extending FragmentActivity. But my application design requires that all activities should extend BaseActivity class.
Solution in my mind:
BaseActivity extend FragmentActivity instead of Activity class.
New activities(with fragments) extend BaseActivity instead of directly extending FragmentActivity.
With this solution I am afraid of any side effect in my existing activities which were extending Activity class (through BaseActivity). Now these activities will extend FragmentActivity (although they are not having fragments inside).
Is it okay if I extend FragmentActivity even though I dont have any fragment inside. Will it behave same as Activity?
as FragmentActivity extends Activity so looks it's fine.jsut you need to add compitiblilty library if want to give the support for old versions
http://developer.android.com/reference/android/support/v4/app/FragmentActivity.html
Even I did same and found no side effect yet

Categories

Resources