karthik
karthik

Reputation: 219

IllegalStateException:Fragment already active

I have a passed data from FragmentActivity to multiple fragments like this

Bundle bundle = new Bundle();
bundle.putParcelableArrayList("userid", logindata);
firstfragment.setArguments(bundle);

From fragment Activity to secong fragment i have pass data like this.

secondfragment.setArguments(bundle);

when i click first fragment it's working next click second fragment it's also working fine but again click first fragment the illegal state exception Fragment already active exception will be occur. How to reslove this problem please help me. Thanks in advance.

Upvotes: 0

Views: 2742

Answers (2)

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132992

illegal state exception Fragment already active

setArguments only called before the fragment has been attached to its activity otherwise through Fragment already active Exception. so call setArguments to should need to remove fragment is already present and use replace to add fragment again.

Remove fragment before adding new :

Fragment fragment = 
               getSupportFragmentManager().findFragmentByTag("firstfragment");
if(fragment != null)
    getSupportFragmentManager().beginTransaction()
                               .remove(fragment).commit();

Now add new fragment :

 firstfragment.setArguments(bundle); 
 transactn = mngr.beginTransaction(); 
 transactn.replace(R.id.content_frag,firstfragment).
                    addToBackStack("firstfragment").commit();

Upvotes: 2

Lucas Moresco
Lucas Moresco

Reputation: 1

Try this:

    createFragment(new YourFragment());

    private void createFragment(Fragment fragment) {
    FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.container_app, fragment).commit();
}

Upvotes: 0

Related Questions