Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing data to added Fragment - IlligalStateException: Fragment already active

I have 2 fragments which is active and showing side by side on a tablet. I need to pass some data from fragmentA to fragmentB when user selects an item from a listview in fragmentA. My problem occurs when trying to get the data from the activity to fragmentB. I'm getting the error: IlligalStateException: Fragment already active.

I pass the data from activity like this:

    @Override
    public void onTrackSelected(String trackId) {

        topBarFragment topBarFragment = (topBarFragment)getSupportFragmentManager().findFragmentById(R.id.topBar_fragment);

        Bundle bundle = new Bundle();
        bundle.putString("trackId", trackId);
        //set Fragmentclass Arguments
        topBarFragment.setArguments(bundle);

        topBarFragment.onTrackSelected();

    }

And then receive in fragmentB like this:

public void onTrackSelected() {

        String trackId = getArguments().getString("trackId");
        Toast.makeText(getActivity().getApplicationContext(), trackId,Toast.LENGTH_SHORT).show();

    }   
}

Logcat:

02-25 02:58:22.190: E/AndroidRuntime(29593): FATAL EXCEPTION: main
02-25 02:58:22.190: E/AndroidRuntime(29593): java.lang.IllegalStateException: Fragment already active
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.support.v4.app.Fragment.setArguments(Fragment.java:500)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at com.harteg.fragmentstest.ItemListActivity.onTrackSelected(ItemListActivity.java:88)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at com.harteg.fragmentstest.TracksFragment.onListItemClick(TracksFragment.java:136)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.support.v4.app.ListFragment$2.onItemClick(ListFragment.java:58)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.os.Handler.handleCallback(Handler.java:725)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.os.Looper.loop(Looper.java:137)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at android.app.ActivityThread.main(ActivityThread.java:5039)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at java.lang.reflect.Method.invokeNative(Native Method)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at java.lang.reflect.Method.invoke(Method.java:511)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-25 02:58:22.190: E/AndroidRuntime(29593):    at dalvik.system.NativeStart.main(Native Method)
like image 873
Jakob Avatar asked Feb 25 '13 19:02

Jakob


1 Answers

You can't change the arguments you passed to the fragment after it has been created. What you should do is just pass the data in your method call, like this:

topBarFragment.onTrackSelected(trackId);

And use it on your fragment:

public void onTrackSelected(int trackId) {
  Toast.makeText(getActivity().getApplicationContext(), String.valueOf(trackId),Toast.LENGTH_SHORT).show();
}

For more information have a look at the documentation here on how to handle communication between fragments.

like image 51
ebarrenechea Avatar answered Nov 15 '22 00:11

ebarrenechea