ChrisMcJava
ChrisMcJava

Reputation: 2293

Where to put the Fragment functional code?

Just a general question about working with Fragments and Activitys for android development: where does the business end of the functional code go for Fragments loaded into an Activity dynamically? (i.e. a fragment's OnClickListeners, OnCheckedChangedListeners, button logic methods...)

Do they go in the Fragment class, or the Activity class?

Upvotes: 1

Views: 832

Answers (4)

Prakash
Prakash

Reputation: 4617

It depends:

If fragment can handle logic which is self sufficient(complete) then that code can be handled by fragment. e.g. on click call phone number.

If fragment have UI whose action is activity specific, then you want to add listener in activity. e.g. master detail view like email client, on tablet user click on title fragment1 which have list of email titles, then handler on click in activity can show detail fragment2 in activity.

In all you want to keep fragment reusable.

Upvotes: 0

Nil
Nil

Reputation: 324

they always in fragment class because fragment is one type of component in android which we can reuse it. if we put onclick and oncheckchanged in activity then what meaning of reusing that component??

for more information about please go through following step:

Link 1 for basic level of information about fragment and how to handle them Link 2 for dealing with multi pane fragment

Standard site for fragment

Upvotes: 0

cYrixmorten
cYrixmorten

Reputation: 7108

All the GUI logic for views attached to a fragment should be contained inside the fragment itself.

Thus a fragment should be as self contained as possible.

You can, though, if necessary do callbacks to your activity based on fragment GUI interaction. This can easily be done like this inside the fragment:

@Override
public void onAttach(Activity activity) {
    if (!(activity instanceof SherlockFragmentActivity)) {
        throw new IllegalStateException(getClass().getSimpleName()
                + " must be attached to a SherlockFragmentActivity.");
    }
    mActivity = (SherlockFragmentActivity) activity;

    super.onAttach(activity);
}

In this specific case the reason for gaining a reference to SherlockFragmentActivity is to gain access to the support menu inflater mActivity.getSupportMenuInflater(), hence the construction can of course also serve to gain information from the underlying activity.

Upvotes: 2

Philipp Jahoda
Philipp Jahoda

Reputation: 51421

This probably depends on how much the Fragment's functionalities have in common, and how many, let's say Buttons, have to be handled.

I personally (and it's probably most common practice) handle onClick(...) events separately for each Fragment, meaning that I let each Fragment implement it's own OnClickListener.

Furthermore, when handling everything through the Activity, probably not all the components that react to click-events are in memory at all times and can be reached via findViewById(...), depending on which Fragment is currently displayed and how your user-interface is built up in general.

Upvotes: 1

Related Questions