I have a project activity (MainActivity) using tabs, each tab launches a fragment (MyFragment). The fragments are not implemented in the project, but are implemented in a referenced library project.
So I want to pass a simple object from the Activity to the Fragment:
private MySimpleClass myObject = new MySimpleClass();
public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) {
static final String KEY = "simpleObject";
final ActionBar actionBar = getActionBar();
Bundle args = new Bundle();
switch( tab.getPosition() ) {
case 0:
fragment = new MyFragment();
args.put??????(KEY, myObject);
fragment.setArguments(args);
fragmentTransaction.add(R.id.fragmentContent, fragment, "tab0_tag");
break;
. . .
}
}
If the Fragment
would not have been in a library I would have bypassed the Bundle
mechanism and just use a local method:
m = ((MainActivity)getActivity()).getMyObject();
But this does not work from the library Fragment code, since it does not recognize MainActivity
and thus cannot cast to it.