How to call fragment from activity without using fragmentactivity? - android

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...

Related

How to get parent activity from Support V4 Fragment?

I am a bit of a noob attempting to pass data between a fragment (living inside a tab layout) and the activity that runs the fragment.
I have found a solution here, but I am unable to call the parent activity from the fragment.
Send data from activity to fragment in Android
Looking up multiple answers, they all say the same thing
CALL getActivity()!!
Call parent's activity from a fragment
It doesn't exist. I am using the Android.Support.V4.App.Fragment.
I can access a property this.Activity, but it's calling a random FragmentActivity when this is being hosted on an AppCompatActivity.
How can I access my hosting AppCompatActivity from the support Fragment?
I would suggest using viewmodels to pass data between activities and fragments

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

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.

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

Is this the right way to call Fragments?

I have this Android project, where from the MainActivity I call some Fragments.
But without including FragmentName.OnFragmentInteractionListeners in the MainActivity I am not able to go to those Fragments.
Public class MainActivity extends AppCompatActivity
implements DirectoryFragment.OnFragmentInteractionListener,
HireDriverFragment.OnFragmentInteractionListener {
What I am worrying about is that I should do this for each Fragment.
If I don't add those Listeners I will get these errors when I try to initiate those Fragments.
must implement OnFragmentInteractionListener
If need more code to provide a better solution I am ready to provide.
What I am worried is that should I do this for every single fragment?
You could create an Interface as Communicating Android Document says, and then you'll have to add the onFragmentInteractionListener on your Fragments.
Also take a look on this Question there are many answers that might help you :)

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