Skip to content Skip to sidebar Skip to footer

Intent Within Fragment Works Only Half The Time

I am trying to pass an intent with data from Activity A to FragmentActivity B to Fragment B. A button from Activity A starts FragmentActivity B. The 'year' string sets a filter wit

Solution 1:

I think this is a thread timing issue, your FragmentActivityB is creating the fragment, which is sent to a thread (I would think) and then you are adding the new value from the original thread, and sometimes the second thread is fast enough to execute FragmentB onCreateView before it gets the new value. What you can do is add the value of 'year' during the FragmentTransaction.

@OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
    // Create a new fragment and bundleFragmentfragment=newImageGridFragment();
    Bundlebundle=newBundle();

    // Put variables in bundle and add to fragment
    bundle.putString("year", getIntent().getStringExtra("year"));
    fragment.setArguments(bundle);

    // Insert the fragmentFragmentManagerfragmentManager= getSupportFragmentManager();
    fragmentManager
            .beginTransaction()
            .add(android.R.id.content, fragment)
            .commit();
}

In FragmentB get the 'year' like this:

year = getArguments().getString("year");

I think this would solve the issue, let me know what happens though,

Cheers.

Post a Comment for "Intent Within Fragment Works Only Half The Time"