0

sorry for my English,

I have quiz application consist of RecyclerView, so when I click to item in that RecyclerView, it should move to questions part which is 5 questions in 5 activities. so I made CountDownTimer for 15 seconds for each question in each activity, when the user answer question than click button, it moves to another activity but the CountDownTimer still running in background, whenever I go back to my RecyclerView, another activity appear with 15 seconds so I don't know why those CountDownTimers do not stop. need help

second_five_questions_questions_1.java

private RadioGroup radioGroup;
private RadioButton radioButton;
private Button btnDisplay;
private TextView mTextField;
private CountDownTimer myCount;
public long val;


@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.second_five_questions_q1);

}


public void addListenerOnButton() {

    radioGroup = (RadioGroup) findViewById(R.id.Rd_Group_Second_five_Questions_q1);
    btnDisplay = (Button) findViewById(R.id.Button_SECOND_btn_Five_Questions_q1);
    btnDisplay.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            // get selected radio button from radioGroup
            int selectedId =radioGroup.getCheckedRadioButtonId();
            myCount.cancel();
            // find the radiobutton by returned id
            radioButton = (RadioButton) findViewById(selectedId);
            if(selectedId==R.id.Rd_btn_three__second_five_questions_q1)
            {
                val++;
                Intent intent = new Intent( second_five_questions_questions_1.this, second_five_questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }

            else
            {
                val = 0;
                //When user Choose Wrong choice
                Intent intent = new Intent( second_five_questions_questions_1.this, second_five_questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);
                finish();
            }
        }

    });

    myCount=new CountDownTimer(16000, 1000) {

        public void onTick(long millisUntilFinished) {
                                                        //Error might be here
            mTextField = (TextView) findViewById(R.id.TimerClock_first_Five_Questions_Q1);
            mTextField.setText("Time left:"+millisUntilFinished / 1000);
            if(millisUntilFinished / 1000 == 5)
            {
                mTextField.setTextColor(Color.RED);
            }
        }


        public void onFinish() {
            val = 0;                                                                            // When time finish go for mainActivity
            Intent intent = new Intent( second_five_questions_questions_1.this, second_five_questions_questions_2.class);
            intent.putExtra("key", val);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            finish();
        }
    }.start();
}}

CustomRecyclerAdapter.java

private Context context;
private List<TestsUtils> TestsUTils;


public CustomRecyclerAdapterTests(Context context, List testutils) {

    this.context = context;
    this.TestsUTils = testutils;
}


@Override
public CustomRecyclerAdapterTests.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.single_list_item_tests, parent, false);
    CustomRecyclerAdapterTests.ViewHolder viewHolder = new CustomRecyclerAdapterTests.ViewHolder(v);
    return viewHolder;
}


@Override
public void onBindViewHolder(CustomRecyclerAdapterTests.ViewHolder holder, int position) {

    holder.itemView.setTag(TestsUTils.get(position));
    TestsUtils pu = TestsUTils.get(position);
    holder.Subject_Title.setText(pu.GetTestName());
    holder.Subject_Description.setText(pu.GetTestSubject());
}

@Override
public int getItemCount() {
    return TestsUTils.size();
}


public class ViewHolder extends RecyclerView.ViewHolder {

    public TextView Subject_Title;
    public TextView Subject_Description;

    public ViewHolder(View itemView) {
        super(itemView);

        Subject_Title = (TextView) itemView.findViewById(R.id.Title_Single_List_Item_Tests);
        Subject_Description = (TextView) itemView.findViewById(R.id.Subject_Single_List_Item_Tests);

        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                switch (getPosition()) {
                    case 0:
                        TestsUtils obj1 = (TestsUtils) view.getTag();
                        Toast.makeText(view.getContext(), "Test-One", Toast.LENGTH_SHORT).show();
                        Intent intentone = new Intent(context, com.example.computer.policeproject.Questions_package.First_Five_Questions.first_five_Questions_questions_1.class);
                        context.startActivity(intentone);
                        break;

                    case 1:
                        TestsUtils obj2 = (TestsUtils) view.getTag();
                        Toast.makeText(view.getContext(), "Test-One", Toast.LENGTH_SHORT).show();
                        Intent intenttwo = new Intent(context, com.example.computer.policeproject.Questions_package.First_Five_Questions.first_five_Questions_questions_1.class);
                        context.startActivity(intenttwo);
                        break;
                }
            }
        });
    }
4
  • share your complete activity and recyclerview adapter class code.
    – Sahil
    May 14, 2018 at 16:49
  • its very big of code maybe can't pest it, am gona try!
    – jassim
    May 14, 2018 at 16:50
  • @Sahil i edited my post
    – jassim
    May 14, 2018 at 16:55
  • am really facing this problem for 6 days now help plz
    – jassim
    May 14, 2018 at 17:00

1 Answer 1

0

Try disabling countdown by adding:

myCount.cancel();

before you finish the activity.

in your code:

@Override
public void onFinish() {
    val = 0;                                                                            // When time finish go for mainActivity
    Intent intent = new Intent( second_five_questions_questions_1.this, second_five_questions_questions_2.class);
    intent.putExtra("key", val);
    intent.addCategory(Intent.CATEGORY_HOME);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    startActivity(intent);
    cancel(); // add this to cancel out countdown
    finish();
}
9
  • where should i add this code brother am newbie in android studio
    – jassim
    May 14, 2018 at 17:04
  • it helped me. Thank you.
    – jassim
    May 14, 2018 at 17:18
  • u saved my job.
    – jassim
    May 14, 2018 at 17:19
  • for example when i reach question number 4 and click back , after few seconds countdowntimer opening the last activity
    – jassim
    May 14, 2018 at 17:37
  • it appears same problem too , when i open first question and than press back to recycler view , countdown timer open the first question and to on ... i think i did not solved my issue
    – jassim
    May 14, 2018 at 17:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.