0

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> 
Community
  • 1
  • 1
Kamajabu
  • 596
  • 1
  • 5
  • 19

1 Answers1

1

bd1 and getArguments() aren't the same thing. bd1 is actually savedInstanceState. If you check bd1 != null then of course there's no saved instance state the first time you start a Fragment. That'd be not null if, for instance, there's a change in the device orientation. On the other hand, getArguments() is set only at first start when the arguments you set in the constructor are passed.

Just remove that if (bd1 != null) and you should be good to go.

Carlo Moretti
  • 2,213
  • 2
  • 27
  • 41
  • 1
    You are right! Thanks! Changing to if(getArguments()!=null) { ... } solved problem. Of course it made new one... There is locked loading gif on bottom of fragment o.0 – Kamajabu May 09 '16 at 14:28
  • is it a `ProgressBar` you put there for loading purposes? – Carlo Moretti May 09 '16 at 14:31
  • Nope, it looks like it is waiting for rest of fragment to load. Something build in ListFragment I think. It looks like that: http://www.tutorialspoint.com/android/images/spinner1.jpg – Kamajabu May 09 '16 at 14:35
  • 1
    Adding setListShown(true); in public void onViewCreated fixed it :) Thanks again! – Kamajabu May 09 '16 at 14:44