How to get parent activity from Support V4 Fragment? - android

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

Related

trying to have a fragment talk to its parent Activity when the fragment is in a share module

I am trying to have my fragment (in a share module) comunicate with its parent activity.
I tried using the following code to call a callback in the parent activity
((MainActivity)getActivity())
I got a error saying MainActivity is undefined. I'm assuming this is because the fragment and Activity are in different modules. (NyLibary and app)
As it's in another module you'll need to import MainActivity class in your fragment.
By the way calling the parent activity this way is not the best thing you can do, in my opinion you should have interface represents the One who should implement your functions, this way you save the modularity for your fragment.

can we pass the activity handle inside the fragment so as communicate to activity from fragment

i am trying to alter the activity content from fragment. for that i want to pass the activity handle inside fragment and do the required changes.
If i can do that why there is more difficult way of interface etc.
You can call getActivity() from the Fragment.
If you want to get your activity specifically you can cast it
((MyActivity) getActivity()).someMethod()
This will tightly couple your Fragment to your activity and prevent you using the fragment in a different activity easily so be careful.
Also you need to be careful with lifecycles and such as a fragment can become detached from an activity causing a NullPointerException from time to time. So it is recommended to wrap this in a null check

onAttach(Activity activity) what is this activity in a fragment inside another fragment

It sounds me a confusing Question. What is the Activity Object in onAttach method of a fragment which is nested inside a parent Fragment?
I know I can communicate to the parent fragment by using getParentFragment() method.But I want to know if this activity object in the onAttach returns the FragmentActivity instance or not, so as to have a direct communication (by using interface callback) from a nested fragment to the FragmentActivity.
Sorry for my bad English. I tried my best to convey my thoughts.
Yes, the Activity object is your FragmentActivity. Check the docs here: http://developer.android.com/reference/android/app/Fragment.html#onAttach(android.app.Activity)
You can also get the activity by using getActivity() anywhere in your fragment (but be aware it only works after onActivityCreated has been called)
Yes it is the FragmentActivity that hosts the Fragment (directly or nested in another Fragment). There is more information about communicating with the parent FragmentActivity here.

DialogFragment in Fragment and callbacks

I have read a lot of documents about how to use fragments but I have one more doubt.
I have one activity that controls 3 fragments, now in one of this fragment I call a DialogFragment and following the google's tutorial I defined an interface to the activity for callbacks. All it's works properly but is this the only way to pass data to the fragment that fired the Dialog? I think that it would be more convenient passing data directly to the fragment instead of passing from the activity. is there any method to do this?
Thank you in advance.

What is the difference between Fragment and FragmentActivity?

My question is apart from the obvious inheritance differences, what are the main differences between Fragment and FragmentActivity? To what scenarios are each class best suited? I'm trying to get an understanding of why both of these classes exist...
A Fragment is a section of an Activity, which has:
its own lifecycle
receives its own input events
can be added or removed while the Activity is running.
A Fragment must always be embedded in an Activity.
Fragments are not part of the API prior to HoneyComb (3.0). If you want to use Fragments in an app targeting a platform version prior to HoneyComb, you need to add the Support Package to your project and use the FragmentActivity to hold your Fragments. The FragmentActivity class has an API for dealing with Fragments, whereas the Activity class, prior to HoneyComb, doesn't.
If your project is targeting HoneyComb or newer only, you should use Activity and not FragmentActivity to hold your Fragments.
Some details:
Use android.app.Fragment with Activity. Use android.support.v4.app.Fragment with FragmentActivity. Don't add the support package Fragment to an Activity as it will cause an Exception to be thrown.
A thing to be careful with: FragmentManager and LoaderManager have separate support versions for FragmentActivity:
If you are using a Fragment in an Activity (HoneyComb and up), call
getFragmentManager() to get android.app.FragmentManager
getLoaderManager() to get android.app.LoaderManager
if you are using a Fragment in a FragmentActivity (pre-HoneyComb), call:
getSupportFragmentManager() to get android.support.v4.app.FragmentManager.
getSupportLoaderManager() to get android.support.v4.app.LoaderManager
so, don't do
//don't do this
myFragmentActivity.getLoaderManager();
//instead do this:
myFragmentActivity.getSupportLoaderManager();
or
//don't do this:
android.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager();
//instead do this:
android.support.v4.app.FragmentManager fm = myFragmentActivity.getSupportFragmentManager()
Also useful to know is that while a fragment has to be embedded in an Activity it doesn't have to be part of the Activity layout. It can be used as an invisible worker for the activity, with no UI of its own.
FragmentActivity is our classic Activity with fragment support, nothing more. Therefore FragmentActivity is needed, when a Fragment will be attached to Activity.
Well Fragment is good component that copy the basic behaviors of Activity, still not a stand-alone application component like Activity and needs to be attached to Activity in order to work.
Look here for more details
Think of FragmentActivity as a regular Activity class that can support Fragments. Prior to honeycomb, an activity class could not supoprt Fragments directly, so this is needed in activities that use Fragments.
If your target distribution is Honeycomb and beyond you can extend off of Activity instead.
Also a fragment is to be considered as a 'sub-activity'. It cannot exist without an activity. Always think of a fragment as a sub-activity and you should be good. So the activity would be the parent and the fragment(s) the child kind of symbolic relationship.
a FragmentActivity is an ad-hoc activity that contains Fragment.
In these few words I have explain you one of the main important changes that, with android 3.0(HoneyComb), android team has inserted in the android sdk.
With these new kind of concept your pieces of code and layout becomes more flexible and maintainable. If you search on google there are a lot of examples.

Categories

Resources