I was trying to pass parameters to my fragment in the way described here. But I have a problem, I get empty bundle (null) every time. Fragment gets connected first in setContentView, then it gets replaced using newInstance (everything works fine there, bundle is created, added and returned in fragment). In both cases in onCreate I get empty bundle. When created at beginning it should be empty, but after replacing with fragment with added bundle in newInstance it shouldn't.
Any ideas?
ListFragment
public class LeftListFragment extends ListFragment {
private FragmentConnector connector;
private FragmentConnector.listType type;
public static final LeftListFragment newInstance(ArrayList<String> values) {
LeftListFragment llf = new LeftListFragment();
final Bundle bdl = new Bundle(1);
bdl.putStringArrayList("values", values);
llf.setArguments(bdl);
return llf;
}
public void onCreate(Bundle bd1) {
super.onCreate(bd1);
if (bd1 != null) {
List<String> tempList = getArguments().getStringArrayList("values");
//due to JVM optimizations, using new String[0] is better than new String[list.size()]
String[] values = tempList.toArray(new String[0]);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), R.layout.rowlayout, R.id.label, values);
setListAdapter(adapter);
}
MenuActivity
public class MenuActivity extends Activity implements FragmentConnector {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ArrayList<String> list = new ArrayList<String>();
list.add("hello");
setContentView(R.layout.activity_menu);
if(savedInstanceState == null){
getFragmentManager()
.beginTransaction()
.replace(R.id.leftListFragment ,LeftListFragment.newInstance(list))
.addToBackStack(null)
.commit();
}
MenuActivity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:baselineAligned="false"
android:orientation="horizontal"
tools:context=".Menu.MenuActivity"
android:layout_height="300dp"
android:layout_width="fill_parent">
<fragment
android:id="@+id/leftListFragment"
class="com.buczel.attapp.Menu.LeftListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
tools:layout="@layout/rowlayout">
</fragment>