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

StackOverflow https://stackoverflow.com/questions/23661165

سؤال

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top