Last active
August 29, 2015 14:04
-
-
Save nein37/b5483800f8a05d9b5378 to your computer and use it in GitHub Desktop.
Fragment使用時のIllegalStateException回避 ref: http://qiita.com/nein37/items/32613e9acd9558566c5e
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Bundle bundle = new Bundle(); | |
// 対象フラグメントに渡したいパラメータはこのタイミングで設定できる | |
bundle.putString(ToFragment.BUNDLE_TOAST_TEXT,"hogehogehogehoge"); | |
// Fragment操作処理を登録 | |
// このタイミングで直接Fragment操作を行うと落ちる場合がある | |
sendMessage(WHAT_REPLACE_FRAGMENT, bundle); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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