8

by this answer I can't understand where to put my onClickListener() - inside onCreateView() or inside onActivityCreated() , below codes describe it better:

CODE A: (Setting Button click listener inside onActivityCreated())

  private FloatingActionButton bt;      

  @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              // do something.
            }
        });
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_frag, container, false);
        bt = (FloatingActionButton) v.findViewById(R.id.fab);
        return v;
    }

CODE B: (Setting Button click listener inside onCreateView())

    private FloatingActionButton bt;      

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_frag, container, false);
        bt = (FloatingActionButton) v.findViewById(R.id.fab);
        bt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              // do something.
            }
        });            
        return v;
    }

I may have not understood which code is better because of my poor English, anyway, thank you all :)

Community
  • 1
  • 1
DAVIDBALAS1
  • 484
  • 10
  • 31
  • In your **CODE A**, you're still initialising `bt` in `onCreateView()` so, I don't see why you don't want to set click listener there too. – Shaishav Aug 28 '16 at 18:54
  • Both will have no effect as far as I know. Once the view is inflated you can put it anywhere either in onCreateView() or in onActivityCreated(). – Krupal Shah Aug 28 '16 at 18:56

3 Answers3

6

Both will have no effect as far as I know. Once the view is inflated you can put it anywhere either in onCreateView() or in onActivityCreated().

After all, for binding views and setting click listeners, onViewCreated() is a better candidate though, as it will be called immediately after onCreateView. It clearly suggests that your view has been inflated.

There is no specific reason or rule for it. Google itself doesn't care much about it. As a rule of thumb, you can put it anywhere you want once the view is inflated.

Krupal Shah
  • 8,949
  • 11
  • 57
  • 93
0

I would suggest putting the onClickListener it inside the onActivityCreated. And binding the button to the view inside onCreateView. Just like you have done the first time in your question.

To read more regarding the methods you can go through this post

Community
  • 1
  • 1
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45
0

Since onActivityCreated was deprecated in API level 28, it's probably wise to put it in onCreateView!

Rautermann
  • 334
  • 3
  • 10