Android: Where to put activity's onCreate() code in a fragment? - java

I'm converting all my Activities to Fragments so that I can use them in a ViewPager.
I've searched for this but I couldn't find a satisfying answer, so that's why I'm asking it here.
In my Activities, I've written some code in the onCreate() method. I for example call some findViewById()s in order to link some xml-buttons to my Activity. I also make some views invisible in the onCreate(), set an OnClickListener(), fill a TextView with text and remove a Notification, all in the onCreate() method.
My question is: Where should I put this code in the fragment? In the onCreate()? onCreateView()? onActivityCreated()? and why?
Many thanks in advance!

Although Pragnani's answer is close, there's little educational value in it. Besides, there's a more appropriate option to his 2nd statement.
Where should I put this code in the fragment? In the onCreate()?
onCreateView()? onActivityCreated()? and why?
The short answer is: either onCreateView() or onActivityCreated() will do. The view hierarchy won't be created until onCreateView(), so that's the earliest point in the fragment's life cycle that you could inflate the views and attach click listeners etc. Since onActivityCreated() will always be run after onCreateView(), that's a suitable location too. onCreate() may be skipped in favour of the system temporarily detaching the fragment and reattaching it, e.g. when retaining fragments.
Pragnani is correct by pointing out that inflating the views of a fragment is slightly different from inflating views in an activity. More specifically: a fragment does not define a findViewById() method, so you'll need to call it on some other object.
Rather than using getActivity().findViewById(), you'll want getView().findViewById(). The reason for this is that if you use the activity for the view lookups, then you'll get into trouble when multiple fragments with the same view IDs are attached to it. This will be the case if you reuse view ids in the layouts of your various fragments, or if you show two identical fragments that display different data. In both cases, only the first match would ever be returned, whereas you really want to the view to be looked up in the conext of the fragment. That's exactly what getView() returns, the fragment's root view (that you returned in onCreateView()), and thus limits the scope of the lookup appropriately.

1.Left the onCreate empty and just call super.onCreate()
2.Instead of findViewById() use getActivity().findViewById()
always use getActivity() where you need context of the view.
Do all other operations in onCreateview()

Related

How to reuse a layout in other fragments

I'm using a Navigation Drawer Activity. When I made two fragments, I noticed that their layout was the same. The difference is that pressing the button executes a different method for each fragment. But the other buttons do the same thing on the two fragments. How do I reuse the layout and java code for the fragments and be able to specify a method to be executed on each fragment, knowing that it will need View access?
One way to do this is to create two different fragment classes that load the same XML layout and assign the click handler for the button programmatically.
you can use visibility of the button.use same xml for both fragment instead of one button use two, then in fragment check which fragment is in view then change the visibility.Gone of the not needed button for the activity. if you are using databindng then do different task for two button in viewmodel that will do the rest of your button task
You can set a Boolean (fals is the first "fragment", true the second). Then you can use a check in witch "fragment" you are and execute the code.
if(true){
executeFirstButtonUse()
}else{
executeSecondButtonUse()
}
Then U wil have 1 fragmen, 1 view, (1 viewModel).
I u coud provide more info or code I could provide more detail or an other method.

Attempt to invoke virtual method View.SetOnClickListener on a null object reference in Android [duplicate]

I know that we need to place setContentView() in the onCreate() method before initializing any view otherwise it will throw a null pointer exception.
But what is the reason for it?Is the setContentView() similar to the inflate() method?
before initializing any view
I do not know for certain what you mean by "initializing any view". Given the rest of your question, I am going to interpret this as meaning "call findViewById() on the activity".
You need to call setContentView() before calling findViewById(), because otherwise there are no widgets to find.
Is the setContentView() similar to the inflate() method?
setContentView() will use a LayoutInflater and inflate() under the covers, if you pass a layout resource ID into the setContentView() method.
If no content View is set then from where you will reference the views like EditText,TextView,ListVIew and all other components which you have used in your layout.
It is like you have items in your bucket and its cover is locked for safety, you came in house without bucket and forgot it in the car and your mom asked you to put items 1 by 1 on Kitchen counter , but you don't have bucket?? so first you will get bucket then you will take out items from it.
Simply first you have to have a Container in your activity so that you can reference its items by using their ID which are assigned in layout xml.
Hope it is clear to you.!
Ok. Agree with #CommonsWare. In some details, let say if you have some views defined in your xml layout file and you want to use those views in you activity, so in this case you have to call setContentView(<R.layout.xml_layout_name>) and after that to inititlalize view using findViewById(R.id.<view_name>) with resource name as your xml layout defined name.
Ok but why we have to call setContentView() ?
So when you call setContentView() application activity means android nutshell will render views and prepare view hierarchy for your activity from layout file. Just remember you have defined views in layout using xml file and you are using those views in Java code, so for that all works to preparing views for you will do setContentView() that's why when you call findViewById() without setContentView() then your application can not find views from view hierarchy and it will throw NullPointerException.
setContentView() similar to the inflate() method ?
Some how, because both are doing the same thing, rendering views from given xml layout files but scope of works is different. setContentView() provides views throughout your activity scope while inflate() will only gives you a view from the layout file, that's why whenever you have used inflate() you have to always use reference of return view to call findViewById() like
pseudo code only for your understanding,
View view = infalter.inflate(<R.layout.<file_name>>);
TextView mextView = view.findViewById(R.id.textView);
And yes, setContentView() uses the same inflater.inflate() method too.
And when setContentView() and inflate() not required?
If you are creating dynamically views, in java code then you don't have to required call either setContentView() or inflate().
Note: In old android version when you create a dynamically views using java code and you pass it to some ListView's header or footer it won't work. For this views must be inflated before set to the ListView.

Order of Fragment Lifecycle Methods

IF i have multiple fragments in my layout, what exactly is the order in which their lifecycle methods are called? Am i right in thinking Fragment 1's onAttach(), onCreate(), onCreateView() is run first followed by the same three methods for Fragment 2? And then each of the fragments onStart() is called in the order in which the fragments are defined in the layout? If anyone can point me to a resource that clearly defines this ordering it would really help!

How to reset a fragment's view?

If I have a fragment where I dynamically set a view with onCreateView(), how would I go about calling it again?
I want to implement some kind of "refresh" where the view changes based on the JSON response. I tried making a new function that does midnightSV.removeAllViews(), but how can I call onCreateView() again?
You can't without detaching and re-attaching the fragment.
If you just want to update the data in the view, you can find those views and refresh them from the existing fragment.
If you really want multiple sets of unique layouts, you can look into using a ViewFlipper for your fragment layout and then call setDisplayedChild() to switch to a specific view.

How to use Android Fragments?

I'm looking at some demo code that shows how to use a Fragment Adapter (Tab Adapter in this case). I'm curious as to what exactly the instantiate() method does. I see it used in the following demo code on this page:
http://developer.android.com/reference/android/support/v4/view/ViewPager.html
(see the getItem() method within the TabsAdapter class)
If I'm reading the demo code correctly, every time a user clicks on one of the tabs, a new Fragment is created? And thus the fragment starts the entire life-cycle again (onAttach()...onCreate()... etc)? This sounds awfully inefficient. I would think that the fragment that will represent the content for each tab should be instantiated only once (perhaps in the addTab() method), and then saved into some collection where it can be fetched when getItem() is called.
Please correct me if I'm mistaken in any of this. I'm trying to better understand how to manage fragments.
My money would be on that the setCurrentItem() function doesn't actually destroy the existing Fragment being shown in that tab. Otherwise there's really not much of a reason for the adapter to have a List of available tabs. Likely, when you switch from one tab to another, setCurrentItem() just detaches the UI from the currently active Fragment (or calls its onPause() method) and then re-attaches the UI for the newly selected Fragment (or calls its onResume() method).
But, if you're in doubt, you could read the source :)
Hope it helps,
David
I was able to find an explanation for my question here

Categories

Resources