私はユーザーが小さなテストを受けることができるようにするアプリケーションを作成しています。したがって、PreTestFragment
(ボタン「開始テスト」)、TestFragment
およびPostTestFragment
(テストの結果を含む)で構成されるTestActivity
があります。 TestFragment
は1つの質問をホストしているフラグメントになりますので、と表示したい場合は、複数回表示します(10としましょう)。同じフラグメントを何度も実行するには?
すべての私は、ユーザーが次のTestFragment
で押す毎にインクリメントされstatic
カウンターを作っていたとまで来ることができ、それはカウンターが10
私はフラグメント管理に新たなんだに達した後、彼は辞めになり、だから、NEXTボタンのonClick()
の実装でいくつかのエラーが起きたような気がします。
static private int current_question;
public void onClick(View view) {
FragmentManager mng = getFragmentManager();
FragmentTransaction fgmTr = mng.beginTransaction();
Fragment currentFgm = mng.findFragmentByTag(FragmentTags.TEST_FTAG.toString());
Fragment nextFgm;
Bundle bundle = currentFgm.getArguments();
if (current_question < 10) { // test still running
current_question++;
nextFgm = mng.findFragmentByTag(FragmentTags.TEST_FTAG.toString());
// ...
// putting needed data in the bundle
// ...
if (nextFgm == null)
nextFgm = new TestFragment();
} else { // test is finished
nextFgm = mng.findFragmentByTag(FragmentTags.POST_FTAG.toString());
// ...
// putting different needed data in the bundle
// ...
if (nextFgm == null)
nextFgm = new PostTestFragment();
}
nextFgm.setArguments(bundle);
fgmTr.replace(R.id.test_body, nextFgm, FragmentTags.POST_FTAG.toString());
fgmTr.addToBackStack(null);
fgmTr.commit();
}
@Override
public void setArguments(Bundle args) {
super.setArguments(args);
// Should I use if-else here too to pass different data in the bundle?
}
それを実行し、「NEXT」私は次のエラーを取得初めて押す:このコードを修正する方法
java.lang.IllegalStateException: Fragment already active
を?
このコードが間違っていますか?あなたはそれが何をすると期待していますか? – Bryan
@ブライアンありがとう!編集したメッセージを確認してください – Robb1
あなたはスワイプビューを使ってこのデザインを再考することができます! – Eenvincible