Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Button setOnClickListener onCreateView() or onActivityCreated() in Fragment

Tags:

android

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 :)

like image 368
DAVIDBALAS1 Avatar asked Aug 28 '16 18:08

DAVIDBALAS1


People also ask

What are the differences between onCreate () onCreateView () and onActivityCreated () in fragments and what would they each be used for?

The onCreate() is called first, for doing any non-graphical initialisations. Next, you can assign and declare any View variables you want to use in onCreateView() . Afterwards, use onActivityCreated() to do any final initialisations you want to do once everything has completed.

What is the difference between onCreate () and onCreateView () lifecycle methods in fragment?

onCreate is called on initial creation of the fragment. You do your non graphical initializations here. It finishes even before the layout is inflated and the fragment is visible. onCreateView is called to inflate the layout of the fragment i.e graphical initialization usually takes place here.

What is onActivityCreated in fragment?

onActivityCreated() is a callback provided to Fragments ostensibly for when the activity is finished being created, but it ends up actually being tied into the Fragment view's lifecycle - it is actually called between onViewCreated() and onViewStateRestored() and can be called multiple times in cases where the Fragment ...

Is onCreate or onCreateView called first?

onCreate() is called to do initial creation of the fragment. onCreateView() is called by Android once the Fragment should inflate a view. onViewCreated() is called after onCreateView() and ensures that the fragment's root view is non-null .


1 Answers

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.

like image 178
Krupal Shah Avatar answered Sep 22 '22 08:09

Krupal Shah