2

i have 3 ArrayList> that I want to pass to 3 fragments. Besides making them static, what is the best approach to do this?

2 Answers 2

3

You can use the setArguments in the Fragment. Take a look at http://developer.android.com/guide/components/fragments.html, basically, you create a Bundle before create your Fragment and then setup as an Argument.

Example from the Android Documentation:

public static class DetailsActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (getResources().getConfiguration().orientation
                == Configuration.ORIENTATION_LANDSCAPE) {
            // If the screen is now in landscape mode, we can show the
            // dialog in-line with the list so we don't need this activity.
            finish();
            return;
        }

        if (savedInstanceState == null) {
            // During initial setup, plug in the details fragment.
            DetailsFragment details = new DetailsFragment();
            details.setArguments(getIntent().getExtras());
            getFragmentManager().beginTransaction().add(android.R.id.content, details).commit();
        }
    }
}

Instead of use getIntent().getExtras(), you create you bundle and set the arguments

Bundle bundle = new Bundle();
bundle.putSerializable(YOUR_KEY, yourObject);
fragment.setArguments(bundle);

And for your Fragment:

public static class DetailsFragment extends Fragment {
    /**
     * Create a new instance of DetailsFragment, initialized to
     * show the text at 'index'.
     */
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

    public int getShownIndex() {
        return getArguments().getInt("index", 0);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        if (container == null) {
            // We have different layouts, and in one of them this
            // fragment's containing frame doesn't exist.  The fragment
            // may still be created from its saved state, but there is
            // no reason to try to create its view hierarchy because it
            // won't be displayed.  Note this is not needed -- we could
            // just run the code below, where we would create and return
            // the view hierarchy; it would just never be used.
            return null;
        }

        ScrollView scroller = new ScrollView(getActivity());
        TextView text = new TextView(getActivity());
        int padding = (int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
                4, getActivity().getResources().getDisplayMetrics());
        text.setPadding(padding, padding, padding, padding);
        scroller.addView(text);
        text.setText(Shakespeare.DIALOGUE[getShownIndex()]);
        return scroller;
    }
}
Sign up to request clarification or add additional context in comments.

Comments

1

You can create listener callback interfaces and implement them in your fragments. Something like this:

@Override
public void onSomeEvent(List<SomeData> data) {
    //do something with data
}

In your activity create this interface:

public interface OnSomeEventListener {
    onSomeEvent(List<SomeData> data);
}

then obtain your fragment by using findFragmentById or findFragmentByTag and assign it to a listener:

this.onSomeEventListener = fragment; 

You can then call methods of that interface and your fragment will receive callbacks.

The second and more easier way of communication between fragments and activities is BroadcastReceivers. You can register some BroadcastReceiver in your fragments and then call sendBroadcast() from activity. Your list of data can be put in a bundle of that broadcast message.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.