Dan Riza
Dan Riza

Reputation: 477

Where is the best place to add programming logic in a fragment ?

I worked with fragments for a while now, and i want to ask you which is the best practice for adding programming logic when using fragments. I usually set click listeners and instantiate adapters etc... inside the onCreateView method right after inflating layout and instantiating views (TextViews EditTexts Lists).

What do you guys think is the best place to add logic within the fragment lifecycle ?

Upvotes: 0

Views: 1647

Answers (2)

kvh
kvh

Reputation: 2158

1.I make layout and register listeners in

  onViewCreated();

2.This following two methods to handle resuming.

public void onAttach(Activity activity) {
    super.onAttach(activity);
    //do something
}

@Override
public void onDetach() {
    super.onDetach();
    //do something
}

Upvotes: 1

10s
10s

Reputation: 1699

I personally use onViewCreated() which according to the documentation is the method that is called after the layout is inflated into the layout.

It helps keeping the code clean and don't have one method implement all the logic.

Upvotes: 2

Related Questions