Fragments without onCreateView - android

recently i saw some articles about inflating the fragment layout directly from the Fragment()
class ExampleFragment: Fragment(R.layout.example_fragment)
does this stand only for fragments that dont hold any data and interaction, when should we use this method

This was added in androidx.fragment version 1.1.0:
Fragment LayoutId constructor: Subclasses of Fragment can now optionally call into a constructor on Fragment that takes an R.layout ID, indicating the layout that should be used for this fragment as an alternative to overriding onCreateView(). The inflated layout can be configured in onViewCreated().
So in essence it's a shorthand for overriding onCreateView() that just inflates a layout and returns it, and encourages a style of configuring the view in onViewCreated() rather than onCreateView().

Related

Question about defining resource layout id in the activity/fragment constructor

I keep reading everywhere that you can now use the activity/fragment constructor to define the layout id with AndroidX, but none answer my question.
What if you have others things to do in Activity's onCreate or Fragment's onCreateView ? Can you still use this new constructor ?
For Activities
Yes, it just means that the Activity will call setContentView() with the layout ID you provide as part of calling super.onCreate(savedInstanceState). Otherwise, you can proceed as normal in your onCreate().
For Fragments
Generally, if using the layout ID constructor, you'd want to move your code from onCreateView() to onViewCreated(), which is passed the View that was created by onCreateView().
Of course, if you do want to override onCreateView() for some reason, you'd want to call super.onCreateView() to retrieve the inflated layout corresponding with the layotu ID you provided to the constructor argument.

Why OnCreateView in fragments?

What is the difference between OnCreate and OnCreateView in the lifecycle of fragments. Also why there is no OnCreateView in Activity life cycle but in frgaments. I am confusion in these two methods.
Please help me with the suggestion as I am very new to android programming.
onCreateView() allows you to inflate a layout for a fragment and get your views with findViewById().
The fragment's onCreate() gets always called directly before the onCreateView() method. It does not allow you to setup a fragment layout, so you always have to override onCreateView().
Most times you do not need the fragment's onCreate().
An activity does not need onCreateView(), since it has the setContentView() method, which allows you to setup a layout in the activity's onCreate().

Difference between getActivity() and view in Fragment

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.

Binding fragments in listview

I have a MvvmCross Android application and let's say it contains some Fragment and ViewModel. I set the ViewModel for the Fragment inside OnCreate call of the Activity which hosts the fragment as it is shown at N+1 Fragments Sample All bindings inside the fragment work fine.
I also have another Activity with a view that contains MvxListView. The row template of the listview contains the fragment I described above. How can I set the ViewModel of the fragment so that bindings work correctly?
We have found that you can't get hold of the MvxListView in the OnCreateView method to bind to the ViewModel.
We have managed to get it work work if you use OnActivityCreated method and bind your ViewModel to the View there.

Android Fragment onCreateView vs. onActivityCreated

I know that a fragment's view hierarchy has to be inflated in onCreateView, but what other functionality can be in onCreateView vs. what should wait for onActivityCreated? My current implementation uses separate activities for everything, and a typical activity does most of its work in its onCreate method, including inflating the view, setting the content view, initializing the various widgets with data, setting up listeners, etc.
So can this probably all be moved into onCreateView, or should some functions be put into an onActivityCreated method instead?
If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you - for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so.
Also accessing the view hierarchy of the parent activity must be done in the onActivityCreated, not sooner.
onActivityCreated() is deprecated in fragment 1.3.0-alpha02 and there is a recommendation to use onViewCreated() instead. View is already created here and you can set listeners, observe LiveData from ViewModel, initialize recyclerView, etc.
For a better understanding, you can take a look at my blog post, where I describe the Android Fragment lifecycle in 137 seconds.

Categories

Resources