I have some problems passing data from an activity to fragments in it. I searched around but didn't find an answer which suit my situation well.
I have 2 fragment class named CurrentFragment.java
and HistoryFragment.java
. I initialize them as tabs in an Activity.
Tab tab = actionBar.newTab()
.setText(R.string.tab_current)
.setTabListener(new TaskitTabListener<CurrentFragment>(
this, "current", CurrentFragment.class));
actionBar.addTab(tab);
tab = actionBar.newTab()
.setText(R.string.tab_history)
.setTabListener(new TaskitTabListener<HistoryFragment>(
this, "history", HistoryFragment.class));
actionBar.addTab(tab);
I was told to use setArguments
in the Activity and getArguments
in the fragments. But in this situation how do I get fragment objects in the Activity? I can't use getFragmentManager().findFragmentById()
since the fragments are added programmatically.
Also, I find some posts saying that I may use getActivity()
in fragments to access data in the Activity container, but for me it keep returning null. Does anyone has a working example of that?