-1

I am really curious about what is the best practice to setup views in a Fragment. This is what I have been doing ever since I started developing for android.

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.main_fragment, container, false);
    ListView listView = view.findViewById(...);
    listView.setAdapter(...);
    return view;
}

I have then read about the method onActivityCreated and read that I need to setup the view inside that and not onCreateView so I wrote my self some code to do the same

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    return inflater.inflate(R.layout.main_fragment, container, false);
}

public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    View view = getView();
    if(view == null){
        throw new NullPointerException("View returned null inside onActivityCreated this shouldn't have happened!");
    }
    ListView listView = view.findViewById(...);
    listView.setAdapter(...);
}

I'm really curious what's the difference here? And which should be used? Which one does android recommend you to use?

SamHoque
  • 2,978
  • 2
  • 13
  • 43
  • Possible duplicate of [Difference and uses of onCreate(), onCreateView() and onActivityCreated() in fragments](https://stackoverflow.com/questions/28929637/difference-and-uses-of-oncreate-oncreateview-and-onactivitycreated-in-fra) – Haroun Hajem Dec 20 '18 at 22:23
  • @HarounHajem This isn't a duplicate, I am not asking about the difference between them. I am asking which is better. – SamHoque Dec 20 '18 at 22:24
  • Then you are asking an opinion based question. We don't question that on SO, all the opinion based questions gets deleted or locked e.g. "Is blue better then red?" – Haroun Hajem Dec 20 '18 at 22:28
  • @HarounHajem that isn't totally true, That's like saying `String += String` rather then using a `StringBuilder` in a loop is opinion based. I am asking which is recommended way to setup views in. :) – SamHoque Dec 20 '18 at 22:37
  • Both setup the view but do it in different order. It's recommended in the document to use the latter for setting up states --> see here https://stackoverflow.com/questions/8041206/android-fragment-oncreateview-vs-onactivitycreated – Haroun Hajem Dec 20 '18 at 22:46
  • What's `Latter`? – SamHoque Dec 20 '18 at 23:00
  • It means the last example. Pointing to `onActivityCreated` – Haroun Hajem Dec 20 '18 at 23:02
  • Okay so I don't really get that answer, What does the person mean by static views? – SamHoque Dec 20 '18 at 23:05
  • @Diffy, he meant by static view that the view which is displayed to the user is nothing but the inflated xml layout. No modification in coding or at runtime. – Haroun Hajem Dec 21 '18 at 23:38
  • They don't hold a state, as the Google documentation describes. It's recommended to restore state in onActivityCreated – Haroun Hajem Dec 21 '18 at 23:40

1 Answers1

1

You can really use whatever you want, as long as it works.

However, I believe onViewCreated() is technically the "official" or recommended way to modify and reference your View after it's been created.

TheWanderer
  • 16,775
  • 6
  • 49
  • 63
  • I see, I see some big applications set it inside `onCreateView` aswell. So I am gonna wait for more people to answer before I accept an answer. – SamHoque Dec 20 '18 at 22:23
  • Since no one answered over 11 days, I doubt anyone will soon. So i will just accept this – SamHoque Dec 31 '18 at 10:59