Skip to content

Instantly share code, notes, and snippets.

@nein37
Last active August 29, 2015 14:04
Show Gist options
  • Save nein37/b5483800f8a05d9b5378 to your computer and use it in GitHub Desktop.
Save nein37/b5483800f8a05d9b5378 to your computer and use it in GitHub Desktop.
Fragment使用時のIllegalStateException回避 ref: http://qiita.com/nein37/items/32613e9acd9558566c5e
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
Bundle bundle = new Bundle();
// 対象フラグメントに渡したいパラメータはこのタイミングで設定できる
bundle.putString(ToFragment.BUNDLE_TOAST_TEXT,"hogehogehogehoge");
// Fragment操作処理を登録
// このタイミングで直接Fragment操作を行うと落ちる場合がある
sendMessage(WHAT_REPLACE_FRAGMENT, bundle);
@Override
public void processMessage(Message message) {
switch (message.what) {
case WHAT_REPLACE_FRAGMENT:
// bundleも取り出す
ToFragment fragment = new ToFragment();
fragment.setArguments(message.getData());
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(getId(), fragment);
ft.addToBackStack("hoge");
ft.commit();
break;
default:
break;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment