In my project context, I have a Button b
in a Fragment f(1)
in an Activity a
.
Fragment f(x)
is an instance of F
where content depends of argument x
I need to replace the current instance f(1)
by an instance f(2)
on b
click event:
From Activity
a
:private void setFragment(int x) { Bundle args = new Bundle(); args.putInt("x", x); F f = new F(); f.setArguments(args); f.setListener(new F.Listener() { public void onButtonClick(int x) { setFragment(x); } }); getSupportFragmentManager() .beginTransaction() .replace(ID, f) .commit(); }
From Fragment
f
:b.setOnClickListener(new View.onClickListener() { public void onClick(View view) { listener.onButtonClick(x + 1); } });
My problem is:
An Exception is throw on b
click event only if a configuration state change occurs:
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Please, what is my error? I read many posts on this Exception but I don't found any solution
Edit: I just make a test without AsyncTask, see the code:
Try to rotate the screen and push the button
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.activity_main);
if (state == null) {
setFragment(1);
}
}
private void setFragment(int id) {
Bundle args = new Bundle();
args.putInt("id", id);
MyFragment myFragment = new MyFragment();
myFragment.setArguments(args);
myFragment.setListener(new MyFragment.Listener() {
@Override
public void onClick(int id) {
setFragment(id);
}
});
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment, myFragment)
.commit();
}
public static class MyFragment extends Fragment{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle state) {
return new Button(getActivity()) {
{
setText(String.valueOf(getArguments().getInt("id")));
setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
listener.onClick(getArguments().getInt("id") + 1);
}
});
}
};
}
private static interface Listener {
public void onClick(int id);
}
private Listener listener;
public void setListener(Listener listener) {
this.listener = listener;
}
}
}