当应用程序从最小化状态打开时,不会调用onActivityCreated

时间:2018-03-20 10:19:39

标签: android

我正在替换活动中的多个片段。使用下面的代码

FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment fragment = new LessonContentFragment();
fragmentTransaction.replace(R.id.flContent, fragment);
fragmentTransaction.commit();

每当我点击移动onSaveInstanceState的主页按钮执行时,但是当我从最小化状态再次打开应用程序时,onActivityCreated不会被执行。可能是什么问题

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if (savedInstanceState != null) {
        //Restore the fragment's state here
        int myInt = savedInstanceState.getInt("MyInt");
        Toast.makeText(getActivity(), myInt, Toast.LENGTH_SHORT).show();
    }
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    //Save the fragment's state here
    outState.putInt("MyInt", 1);
}

我的片段中有以下代码。但当应用程序未处于恢复状态且onfinish出错时。怎么解决这个?

new CountDownTimer(assmt_time, 1000) { // adjust the milli seconds here
                    public void onTick(long millisUntilFinished) {
                        timer.setText("" + String.format("Time left: " + "%02d:%02d",
                                TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished),
                                TimeUnit.MILLISECONDS.toSeconds(millisUntilFinished) -
                                        TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toMinutes(millisUntilFinished))));
                        if (millisUntilFinished < 10000) {
                            if (blink_flag != 1) {
                                blink_flag = 1;
                                blink();
                            }
                        }
                        if (millisUntilFinished < assmt_time10per) {
                            if (colour_flag != 1) {
                                colour_flag = 1;
                                timer.setTextColor(Color.parseColor("#D50000"));
                            }
                        }
                    }

                    //"%02d:%02d:%02d"
                    public void onFinish() {
                        //timer.setText("done!");
                        of.setForceSubmit(1);
                        FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
                        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                        Fragment fragment = new SubmitFragment();
                        fragmentTransaction.replace(R.id.flContent, fragment);
                        fragmentTransaction.commit();
                    }
                }.start();

0 个答案:

没有答案