user5949092
user5949092

Reputation:

Android(Fragment) - Is it recommended to initialize a view object inside onActivityCreated method?

View variables of fragments can be initialised in onCreateViewmethod in this way.Here the TextView is inside the fragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmant_two,container,false);
    if(savedInstanceState == null)
    {

    }
    else
    {
        String data = savedInstanceState.getString("data");
        TextView myText = (TextView)view.findViewById(R.id.text_view);
        myText.setText(data);
    }


    return view;


}

But I found that a view is initialized inside onActivityCreated in this way.

textView = (TextView) getActivity().findViewById(R.id.text_view);

is there any reason for which I should choose onActivityCreated over onCreateView ?

Upvotes: 4

Views: 10899

Answers (5)

Mustafa Aslan
Mustafa Aslan

Reputation: 58

In onCreateView, you may not be able to use getActivity.findViewByID()

But you can in onActivityCreated.

I also suggest using onView Created(). You can use getActivity.findViewByID() here too.

Upvotes: 0

Khoa Tran
Khoa Tran

Reputation: 528

You can refer to this question to understand more about onActivityCreated and onCreateView here

onCreate():

The onCreate() method in a Fragment is called after the Activity's onAttachFragment() but before that Fragment's onCreateView(). In this method, you can assign variables, get Intent extras, and anything else that doesn't involve the View hierarchy (i.e. non-graphical initialisations). This is because this method can be called when the Activity's onCreate() is not finished, and so trying to access the View hierarchy here may result in a crash.

onCreateView():

After the onCreate() is called (in the Fragment), the Fragment's onCreateView() is called. You can assign your View variables and do any graphical initialisations. You are expected to return a View from this method, and this is the main UI view, but if your Fragment does not use any layouts or graphics, you can return null (happens by default if you don't override).

onActivityCreated():

As the name states, this is called after the Activity's onCreate() has completed. It is called after onCreateView(), and is mainly used for final initialisations (for example, modifying UI elements).

Upvotes: 2

Abhishek Jain
Abhishek Jain

Reputation: 3702

textView = (TextView) getActivity().findViewById(R.id.text_view);

This will try to find the view with id R.id.text_view from the layout of your activity, not your fragment. If the view with that id is present in your fragment itself then you should use the onCreateView method within your fragment.

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragmant_two,container,false);
    TextView textView = (TextView) view.findViewById(R.id.text_view);
    // bind your data here.
    return view;
}

Upvotes: 6

OneCricketeer
OneCricketeer

Reputation: 191691

Obviously onActivityCreated was used because the view is found on the Activity.

You could also use onAttach for that, though.

Personally, onViewCreated is where I put my Fragment view initializations

Upvotes: 2

denis_lor
denis_lor

Reputation: 6547

onCreateView(): You can assign your View variables and do any graphical initialisations here.

onActivityCreated(): It is called after onCreateView(), and is mainly used for final initialisations (for example, modifying UI elements).

Upvotes: 0

Related Questions