Question

I have been unable to send data via an Intent or Bundle to my Fragment. Am I missing something that other posts don't seem to mention?

Here is my MainActivity.java:

public class MainActivity extends Activity implements MainFragment.Test {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        Bundle b = new Bundle();
        b.putInt("number", 3);
        MainFragment mf = new MainFragment();
        mf.setArguments(b);
    }

    @Override
    public void testPrint(String s) {
        Log.d("number", s);
    }

}

MainFragment.java:

public class MainFragment extends Fragment {
    Test test;

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        if (activity instanceof MainActivity)
            test = (Test) activity;
        else
            throw new ClassCastException("error");
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_main, container, false);
        Button b = (Button) view.findViewById(R.id.button);
        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final String num = String.valueOf(getArguments().getInt("number"));
                test.testPrint(num);
            }
        });
        return view;
    }

    public interface Test {
        public void testPrint(String s);
    }

}

activity_main2.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <fragment
        android:id="@+id/fragment"
        android:name="com.ygutstein.testfrags.MainFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>
Était-ce utile?

La solution

You're creating a new instance of MainFragment rather than using the one defined in your activity_main2 layout.

But even if you referenced it correctly, you would still throw an IllegalStateException because you can't add arguments to a Fragment that's defined in your xml. Use a FrameLayout instead and call FragmentTransaction.replace.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <FrameLayout
        android:id="@+id/fragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

    final Bundle args = new Bundle();
    args.putInt("number", 3);

    final MainFragment mf = new MainFragment();
    mf.setArguments(args);

    getFragmentManager().beginTransaction().replace(R.id.fragment, mf).commit();
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top