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.
Related
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().
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?
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.
I have read several documents about this but still fail to conclude where should I write my codes to set some values in textviews/edittext ...
what i have read and seen in video tutorials is, both onStart and onActivityCreated methods get called with different actions (like after fragment initiated, or orientation changed etc.). Moreover, both of them get called after Activity's onCreate method, which means views are available from both Fragment methods.
Anybody can give me some advise regarding this?
(p.s. Currently I put all codes accessing xml views inside onStart, and my application is running without any issue)
I don't know of any potential problems with accessing your layout's views in onStart or onActivityCreated.
Personally, I usually set references to my layout's views and set initial values in a fragment's #onViewCreated(). This is the first opportunity after the layout has been inflated that you have to access a layout's children. The View that was inflated is passed as a parameter so you even have direct access to the parent layout object if you needed it for some reason.
According to the fragment lifecycle onActivityCreated() will be called next and then onStart(). All of these will be executed in that same order when a fragment is returned from the back stack -- so it seems to be personal preference.
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.