jul jul - 1 year ago 222
Android Question

How to clear arguments in Fragment?

I have an

to which I pass some url with setArguments().

If I have an
key in the
of my instance of
, I start a service that plays the audio stream. If there was already a stream playing, I stop it and start it again with the new stream.

If there is no arguments, I do nothing.

My problem is that the arguments are kept with the instance, so when I get back to it,
returns the latest arguments set, if no new have been set.

So after reading them, I try to set the arguments to null with


But I get

Java.lang.IllegalStateException: fragment already active

How can I clear the arguments then?

Answer Source

Make sure you are reading getArguments() in onCreate() method of the fragment. Once the fragment was created, when going back you souldn't pass trough onCreate(), so you shouldn't be able to read again the arguments.

If this still does not work, you could make use of a boolean flag and read the arguments only once.

Something like this:


    // read arguments

    argumentsRead = true;
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download