Difference between getActivity() and view in Fragment - android

What is the difference between
getActivity().findViewById(...)
and
View view = inflater.inflate(R.layout.fragment_fragment_v, null);
view.findViewById(...)
in Fragment (when converting Activity to Fragment)?

The difference is that with getActivty.findViewById(...) you are finding views in the scope of activity (activity's layout). With iflater.inflate(R.layout.fragment_fragment_v, null); view.findViewById(...) you are inflating the layout of your fragment and then finding view's in that layout.
But since your fragment is attached to the activity, you will find the view bothways, but I suggest you are finding view's for your fragment in your fragment's scope since there may be several fragments that have common layouts meaning there may be several view's associated with the same ID and that makes the getActivity().findBiewById(...) method unreliable

When you call findViewById with getActivity() you are telling Android to find the View within the Activity's layout, since Fragments are hosted inside Activities it will return you the correct View if its in the hierarchy.
By calling findViewById with the inflated View or in fact the getView() method provided by the framework you are essentially telling Android to find that View within the Fragment's view hierarchy, this should be your preferred approach as the findViewById algorithm will have to look for your View from a lesser set of View's and hence the search will be faster.
I'll leave you with this explanation about getView() from the developer docs.
getView()
Get the root view for the fragment's layout (the one returned by onCreateView(LayoutInflater, ViewGroup, Bundle)), if provided.

Related

Why does onCreateView return the view that is inflated by it?

I know onCreateView inflates the View of a fragment, but why does it also return the View?
What is the returned View ever used for?
The Fragment Manager of the activity the fragment is hosted by calls all fragment lifecycle methods, so does this mean the Fragment Manager uses the returned View from onCreateView? If so, how?
What is the returned View ever used for?
It is used to allow the framework to display that portion of the UI. Otherwise, those widgets would be created, then ignored.
so does this mean the Fragment Manager uses the returned View from onCreateView?
Yes.
If so, how?
The fragment system calls addView() on the ViewGroup that will contain the view managed by the fragment.

Android Development - Find Text View Inside of a fragment that's inside of a TabLayout

I realize views cannot be "found" if they're not a direct child of the layout you're looking from. I have an Activity, which has a ViewPager inside of it. The 0th tab or view inside of the ViewPager has a my_fragment.xml inflated in it. That fragment then has a TextView inside of it. How can i access it from my main/root activity. Also, in the onCreate method of the fragment i've put a line of code that just calls setText on the TextView, and that doesn't seem to be called at all. Any ideas on all of this?
To access a view you need not access every parent / super parent of it. The parent or any other parent up in the view hierarchy is enough. If you get the reference to any parent/super parent of the child view, you can call View.findViewById() to get the child.
View.findViewById() - Look for a child view with the given id. If this view has the given id, return this view.
Don't try to setText in the onCreate of Fragment. You need to do this after the fragment inflates the view. This happens only in the onCreateView. So you need to do the setText in the onCreateView method or in any other method after this one in the lifecycle (preferably onActivityCreated()).
To access the fragment class instance itself from the activity, you can refer this: Is it possible to access the current Fragment being viewed by a ViewPager?

what is the difference between View.findViewById() and Activity.findViewById()?

I am new to android programming i have confusion between View.findViewById() and Activity.findViewById(), when do we have to call these methods and in which class findViewById() method resides i.e in View class or Activity class.
Any help will be appreciated.
The View version searches the view you call it on and all subviews of it. The Activity one searches the top level view set in setContentView and all subviews of it. The Activity one is equivalent to calling View.findViewById on the contentView of the activity.
Its basically a convenience method to make it easier to search for an id within all the views of an activity without having to remember what the top view in it is.

How to get the fragment that spawned a View?

LayoutInflater is a a really powerful tool that does really cool things; such as inflate a Fragment from a layout resource and properly handle its life-cycle. For example:
<fragment class="com.test.TestFrag".../>
when taking the above layout and running it through the LayoutInflater, you will get the view that is spawned by Fragment. You can then add this view to any ViewGroup and have an instance of a fragment running around that still follows the Fragment life-cycle. My question is, how do you get the parent Fragment from this created view?
There really is no mechanism, at run-time, for a fragment to know what ViewGroup is hosting it. That said, since your class would be extending the base Fragment class, there's nothing to stop you from adding an instance variable to your Fragment to keep track of the ID of it's parent. If your fragment is added into the view hierarchy with a FragmentTransaction, then you have to specify which container would host it, since FragmentTransaction::add requires the ID of the ViewGroup that will contain the fragment. If you're adding it to the ViewHierarchy using the XML tag, I don't see why you need to determine its parent at run-time since it isn't being added dynamically anyways.

what does description of `container parameter for function onCreateView of fragment class` mean?

The description of container parameter for function onCreateView of fragment class says :
container : If non-null, this is the parent view that the fragment's UI should be attached to. The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view.
does the line
The fragment should not add the view itself, but this can be used to generate the LayoutParams of the view
mean that container.addView(some_view) is not permitted?
Correct. You would use this parameter in a call to inflate() on a LayoutInflater (e.g., inflater.inflate(R.layout.frag, container, false);) or to determine what LayoutParams are needed if you are creating your fragment contents directly via Java instead of inflation. Otherwise, the container is owned by the hosting activity, and your fragment should leave it alone, AFAIK.

Categories

Resources