0

I am new to android and want to know that in which lifecycle method it would be best to load views in fragment like

editText_full_name  = (EditText) getActivity().findViewById(R.id.editText_full_name);

I am thinking of onResume() but something tells me its not appropriate. Nonetheless I want to know about the best practice of doing so. Also I am using an interface as a communication callback from fragment to activity.

EDIT: Also i wanna know when to attach listeners, like onClickListeners to button in fragment lifecycle.

Salim
  • 143
  • 11

5 Answers5

2

Check Complete Android Fragment & Activity Lifecycle.

You may want to add listener in Fragment.onViewCreated() when it's guaranteed that view hierarchy is build.

Maxim G
  • 1,479
  • 1
  • 15
  • 23
1

It depends on what you are trying to achieve.

first of all, there is a function public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saveInstanceState). There, a view initialisation can be done with the help of LayoutInflater. To get the reference to the child views, you have to call method findViewById on the view returned from LayoutInflater initialisation. In this function also You are provided with the Bundle argument, which can help you react to configuration changes (ie. screen rotating). If the Bundle argument is not null, then the fragment is recreated after configuration change.

Then, there is a function public void onViewCreated(View view...). There, You also can init views. It is called every time, the activity opens the fragment.

As for the question regarding the performance, I sent You to this Stackoverflow answer

Don't init views in onResume as this event is called every time the comes to foreground (sliding down and up notification panel, coming from background, screen on and off).

Community
  • 1
  • 1
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
1

The right answer is to bind views in onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState). Btw, I suggest you to have a look here. It's a fantastic tool for binding Views. It's clear & simple, you won't need to make a lot of statements, just declare the variable as Fragment variables using @BindView(R.id.myId) & add Butteknife.bind(this) in activities, and Butterknife.bind(this, rootView) in fragments.

Luca Nicoletti
  • 2,265
  • 2
  • 18
  • 32
0

onCreateView method is what you are looking for.

Radwa
  • 325
  • 4
  • 21
0
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_layout,
                container, false);

        Button button = (Button) rootView.findViewById(R.id.buttin_id);
        button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

              //.....

              }
           });
     }
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41