CountDownTimer not starting in onResume()

770 Views Asked by At

I am using a CountDownTimer in a Fragment. I want the timer to pause when app goes in background and resume when user comes back. As suggested in other SO questions, I cancelled the timer in onPause() of hosting activity and started a new timer in onResume() from the same point of time (maintained a static variable millisLeft).

I don't know why but timer is not starting again in onResume() (I tried logging in onResume() but no log appeared meaning timer has never started). Please help me.Thanks.

public class MainFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener {

    private View rootView;
    private ComprehensionDb db;
    CountDownTimer countDownTimer;
    static long millisLeft;
    TextView timer;

    public static Fragment newInstance() {
        return new MainFragment();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        rootView = inflater.inflate(R.layout.main, container, false);
        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        toolbar.setBackgroundColor(ContextCompat.getColor(getActivity(), R.color.colorPrimary_comprehension));
        toolbar.inflateMenu(R.menu.menu_main_white);

        toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(MenuItem menuItem) {
                switch (menuItem.getItemId()) {
                    case R.id.action_about:
                        AlertDialog.Builder builder =
                                new AlertDialog.Builder(getActivity());
                        builder.setTitle(String.format("%1$s", getString(R.string.comprehension_about_us)));
                        builder.setMessage(getResources().getText(R.text));
                        builder.setPositiveButton("OK", null);
                        AlertDialog welcomeAlert = builder.create();
                        welcomeAlert.show();
                        assert welcomeAlert.findViewById(android.R.id.message) != null;
                        assert welcomeAlert.findViewById(android.R.id.message) != null;
                        assert ( welcomeAlert.findViewById(android.R.id.message)) != null;
                        ((TextView) welcomeAlert.findViewById(android.R.id.message)).setMovementMethod(LinkMovementMethod.getInstance());
                        break;
                    default: //do nothing
                        break;
                }
                return true;
            }
        });

        DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                getActivity(), drawer, toolbar, R.string.comprehension_navigation_drawer_open, R.string.comprehension_navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) rootView.findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


        db = new ComprehensionDb(getActivity());
        db.open();
        db.resetCount();

        Cursor cursor = db.getMetaCursor();
        cursor.moveToFirst();
        String title = cursor.getString(Constants.COL_TITLE);
        toolbar.setTitle(title);
        String passage = cursor.getString(Constants.COL_PASSAGE);
        final long time = cursor.getLong(Constants.COL_TIME);
        timer = (TextView) rootView.findViewById(R.id.timer);
        assert timer != null;
        timer.setText(String.valueOf(time));

         countDownTimer = new CountDownTimer(time * 1000, 1000) {

            public void onTick(long millisUntilFinished) {
                millisLeft = millisUntilFinished;
                long min = millisUntilFinished / 60000;
                long sec = millisUntilFinished / 1000 - min * 60;
                timer.setText(String.format(Locale.getDefault(), "%1$d:%2$02d", min, sec));
            }

            public void onFinish() {

                Bundle arguments = new Bundle();
                arguments.putString(Intent.EXTRA_TEXT, "1");

                Fragment frag = QuestionFragment.newInstance();
                frag.setArguments(arguments);
                if(getActivity()!=null) {
                    getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                    getActivity().getSupportFragmentManager().beginTransaction().replace(((ViewGroup) getView().getParent()).getId(), frag).addToBackStack(null).commit();
                }
            }
        }.start();

        Menu m = navigationView.getMenu();
        SubMenu topChannelMenu = m.addSubMenu("Questions");
        long numQues = db.getCountQuestions();

        for (int i = 1; i <= numQues; i++) {
            topChannelMenu.add(String.format(Locale.getDefault(), "Question %1$d", i));
            topChannelMenu.getItem(i - 1).setIcon(R.drawable.ic_assignment_black_24dp);
            final int finalI = i;
            topChannelMenu.getItem(i - 1).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem item) {

                    Bundle arguments = new Bundle();
                    arguments.putString(Intent.EXTRA_TEXT, String.valueOf(finalI));
                    countDownTimer.cancel();
                    Fragment frag = QuestionFragment.newInstance();
                    frag.setArguments(arguments);
                    getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

                    getActivity().getSupportFragmentManager().beginTransaction().replace(((ViewGroup) getView().getParent()).getId(), frag).addToBackStack(null).commit();
                    return false;
                }
            });
        }

        MenuItem mi = m.getItem(m.size() - 1);
        mi.setTitle(mi.getTitle());

        ((TextView) rootView.findViewById(R.id.passage)).setText(passage);
        rootView.findViewById(R.id.go_to_ques).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Bundle arguments = new Bundle();
                arguments.putString(Intent.EXTRA_TEXT, "1");
                countDownTimer.cancel();
                Fragment frag = QuestionFragment.newInstance();
                frag.setArguments(arguments);
                getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);

                getActivity().getSupportFragmentManager().beginTransaction().replace(((ViewGroup) getView().getParent()).getId(), frag).addToBackStack(null).commit();

            }
        });

        return rootView;
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        db.close();
        ViewGroup mContainer = (ViewGroup) getActivity().findViewById(R.id.container);
        mContainer.removeAllViews();
    }

    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        DrawerLayout drawer = (DrawerLayout) rootView.findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

    @Override
    public void onResume() {
        if(countDownTimer==null)
            countDownTimer = new CountDownTimer(millisLeft * 1000, 1000) {

                public void onTick(long millisUntilFinished) {
                    long min = millisUntilFinished / 60000;
                    long sec = millisUntilFinished / 1000 - min * 60;
                    Log.e("sachin",""+millisUntilFinished);
                    timer.setText(String.format(Locale.getDefault(), "%1$d:%2$02d", min, sec));
                }

                public void onFinish() {

                    Bundle arguments = new Bundle();
                    arguments.putString(Intent.EXTRA_TEXT, "1");

                    Fragment frag = QuestionFragment.newInstance();
                    frag.setArguments(arguments);
                    if(getActivity()!=null) {
                        getActivity().getSupportFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                        getActivity().getSupportFragmentManager().beginTransaction().replace(((ViewGroup) getView().getParent()).getId(), frag).addToBackStack(null).commit();
                    }
                }
            }.start();
        super.onResume();
    }

    @Override
    public void onPause() {
        countDownTimer.cancel();
        super.onPause();
    }

}
1

There are 1 best solutions below

1
On BEST ANSWER

In your onPause you are doing countDownTimer.cancel(); not setting countDownTimer to null. Either set it to null and try or remove the null check in onResume