таймеры обратного отсчета начались до достижения деятельности - PullRequest
0 голосов
/ 14 мая 2018

У меня проблема с таймером обратного отсчета, мое приложение состоит из повторного просмотра предметов, я пытаюсь выполнить тест приложения, поэтому, когда я нажимаю на элемент в обзоре повторного использования, запускается первое действие, которое является первым вопросом с таймером обратного отсчета, когда я нажмите кнопку, чтобы перейти от текущей деятельности к другой, с которой она работает. но когда я нажму кнопку «Назад», чтобы просмотреть другие 4 вопроса, они пообедают один за другим с таймерами обратного отсчета.

first_five_Questions_questions_1

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.first_five_questions_q1);
    addListenerOnButton();

}


public void addListenerOnButton() {

    radioGroup = (RadioGroup) findViewById(R.id.Rd_Group_first_five_Questions_q1);
    btnDisplay = (Button) findViewById(R.id.Button_first_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__first_five_questions_q1)
            {
                val++;
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_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
            {

                //When user Choose Wrong choice
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_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) {

            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() {
                                                                                       // When time finish go for mainActivity
            Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
            intent.putExtra("key", val);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            cancel();
            finish();
        }

    }.start();
}}

CustomRecyclerAdapterTests

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.Second_Five_Questions.second_five_questions_questions_1.class);
                        context.startActivity(intenttwo);
                        break;
                }
            }
        });
    }
}

}

if(selectedId==R.id.Rd_btn_three__first_five_questions_q1)
            {
                val++;
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_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
            {

                //When user Choose Wrong choice
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
                intent.putExtra("key", val);
                intent.addCategory(Intent.CATEGORY_HOME);
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(intent);

                finish();
            }

1 Ответ

0 голосов
/ 14 мая 2018

добавьте этот метод в свою деятельность

@Override
public void onBackPressed() {
  myCount.cancel();
}

вот модифицированный класс активности вопроса.посмотрите, куда я вставил метод onBackPressed (), скопируйте его во все ваши вопросы.

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.first_five_questions_q1);
    addListenerOnButton();

}


public void addListenerOnButton() {

    radioGroup = (RadioGroup) findViewById(R.id.Rd_Group_first_five_Questions_q1);
    btnDisplay = (Button) findViewById(R.id.Button_first_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__first_five_questions_q1)
            {
                val++;
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_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
            {

                //When user Choose Wrong choice
                Intent intent = new Intent( first_five_Questions_questions_1.this, first_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) {

            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() {
                                                                                       // When time finish go for mainActivity
            Intent intent = new Intent( first_five_Questions_questions_1.this, first_five_Questions_questions_2.class);
            intent.putExtra("key", val);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
            cancel();
            finish();
        }

    }.start();
}

@Override
public void onBackPressed() {
  super.onBackPressed();
  myCount.cancel();
}

}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...