如何完全结束或终止android中的活动并过渡到另一个活动?

问题描述

我知道这个问题被问了好几次,但我似乎无法使用提供的任何答案来解决它。我只是想点击一个按钮来结束我当前正在进行的活动并转到另一个活动。我也想从后台堆栈中删除它,所以我在我的警报对话框中执行了以下操作:

    private void AlertMessage()
{
    AlertDialog alertDialog = new AlertDialog.Builder(this,android.R.style.Widget_Material_buttonbar_AlertDialog)
//set icon
            .setIcon(android.R.drawable.ic_dialog_alert)
//set title
            .setTitle("YOU ARE ABOUT TO EXIT!!!")
//set message
            .setMessage("Exiting will cancel this process")
//set positive button
            .setPositiveButton("Yes",new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface,int i) {
                    //set what would happen when positive button is clicked
                    //RESETTING FirsT THEN RETURN TO MAIN MENU
                    if (Build.VERSION.SDK_INT >= 26)
                    {
//                        recreate();
//                        pb.setProgress(0);
                        Toast.makeText(tool1mode1.this,"Exit",Toast.LENGTH_SHORT).show();
                        Intent goingback = new Intent(tool1mode1.this,Settings.class);
                        goingback.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_NO_HISTORY);
                        finishAffinity();
                        tool1mode1.this.finish();
                        overridePendingTransition(R.anim.slide_in_left,R.anim.slide_out_right);
                        startActivity(goingback);
                    }
                }
            })
//set negative button
            .setNegativeButton("No",int i) {
                    //set what should happen when negative button is clicked
                    Toast.makeText(getApplicationContext(),"Cancelled",Toast.LENGTH_LONG).show();
                }
            })
            .show();
}

然而,当我在我以为我结束了自己的活动后确实去了另一项活动时,我仍然从本应结束的活动中得到了一些东西。我在我参与的活动中触发了振动和 Toast 消息,即使它本应结束,它们仍然不断出现。

那我需要做什么?顺便说一下,此应用是一款可穿戴应用,以防万一它的工作方式与手机不同

简而言之,我想一劳永逸地结束活动并将其从后堆栈中清除。

编辑:关于系统服务,我在倒计时期间的不同时间触发振动

        private void startTimer() {
        Log.println(Log.ASSERT,"CHECK","Entered startTimer() method");
        millisInFuture = mTimeLeftInMillis;
        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis,1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();
                millisPassed = millisInFuture - mTimeLeftInMillis;
                progress = (int) ((millisPassed * 100 / millisInFuture));
                pb.setProgress(progress);
//                pb2.setProgress(0);
//                pb3.setProgress(0);

/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //Key: 60 sec
                if (millisInFuture == 480000) {
                    if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,R.anim.fade_in);
//                        stepupimage.setAnimation(animation);

                        Log.println(Log.ASSERT,"Check that the first if statement of key 60 is entered");
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 60000;
//                        progress2 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"TIME OF STAGE = "+time_of_stage);
//                        Log.println(Log.VERBOSE,"progress2= "+progress2);
//                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();

                        setflowrate();

                    } else if ((millisPassed > 60000 && millisPassed <= 180000)) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,R.anim.fade_in);
//                        stepdownimage.setAnimation(animation);

                        Log.println(Log.ASSERT,"Check that the second if statement of key 60 is entered");
                        statusIfDown();

                        time_of_stage = (millisInFuture - (millisPassed+60000)) % 120000;    //CREDIT GOES TO BASHMOHNDES AMIN
//                        progress3 = (int) (time_of_stage*100 / 120000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();

                    } else if ((millisPassed > 240000 && millisPassed <= 300000) || (millisPassed > 360000 && millisPassed <= 420000)){
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,"Check that the first if statement of key 60 is entered");
                        statusIfDown();

                        time_of_stage = (millisInFuture - millisPassed) % 60000;
//                        progress3 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();
                    }

                }
                //key:90 sec ----> 01:30
                else if (millisInFuture == 720000) {
                    if ((millisPassed <= 90000) || (millisPassed > 270000 && millisPassed <= 360000)
                            || (millisPassed > 450000 && millisPassed <= 540000)
                            || (millisPassed > 630000 && millisPassed <= 720000)) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,R.anim.fade_in);
//                        stepupimage.setAnimation(animation);
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 90000;
//                        progress2 = (int) (time_of_stage*100 / 90000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
//                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();
                        setflowrate();
                    } else if ((millisPassed > 90000 && millisPassed <= 270000)) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture+90000) - millisPassed) % 180000;
//                        progress3= (int) (time_of_stage*100 / 180000);
                        Log.println(Log.VERBOSE,"TIME OF STAGE = "+time_of_stage);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();

                    } else if ((millisPassed > 360000 && millisPassed <= 450000) || (millisPassed > 540000 && millisPassed <= 630000)) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this,R.anim.fade_in);
//                        stepdownimage.setAnimation(animation);
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 90000;
//                        progress3= (int) (time_of_stage*100 / 90000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }
                }
                //key:120 sec ----> 02:00
                else if (millisInFuture == 960000) {
                    if (millisPassed <= 120000 || millisPassed > 360000 && millisPassed <= 480000 || millisPassed > 600000 && millisPassed <= 720000 || millisPassed > 840000 && millisPassed <= 960000) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 120000;
//                        progress2 = (int) (time_of_stage*100 / 120000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
//                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();
                        setflowrate();

                    } else if (millisPassed > 120000 && millisPassed <= 360000) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture+120000) - millisPassed) % 240000;
//                        progress3= (int) (time_of_stage*100 / 240000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }  else if (millisPassed > 480000 && millisPassed <= 600000 || millisPassed > 720000 && millisPassed <= 840000) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 120000;
//                        progress3= (int) (time_of_stage*100 / 120000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }
                }
                //key:20 sec ----> 00:20
                else if (millisInFuture == 160000) {
                    if (millisPassed <= 20000 || millisPassed > 60000 && millisPassed <= 80000 || millisPassed > 100000 && millisPassed <= 120000 || millisPassed > 140000 && millisPassed <= 160000) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 20000;
//                        progress2 = (int) (time_of_stage*100 / 20000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
//                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();

                        setflowrate();
                    } else if (millisPassed > 20000 && millisPassed <= 60000) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture+20000) - millisPassed) % 40000;
//                        progress3 = (int) (time_of_stage*100 / 40000);
                        Log.println(Log.VERBOSE,"progress2= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();
                    } else if (millisPassed > 80000 && millisPassed <= 100000 || millisPassed > 120000 && millisPassed <= 140000) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 20000;
                        progress3 = (int) (time_of_stage*100 / 20000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();
                    }
                }
                //key:30 sec ----> 00:30
                else if (millisInFuture == 240000) {
                    if (millisPassed <= 30000 || millisPassed > 90000 && millisPassed <= 120000 || millisPassed > 150000 && millisPassed <= 180000 || millisPassed > 210000 && millisPassed <= 240000) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 30000;
//                        progress2 = (int) (time_of_stage*100 / 30000);
                        Log.println(Log.VERBOSE,"progress2= "+progress2);
//                        pb2.setProgress(progress2);
                        updateStageUpCount();
                        upArrowAnimation();
                        setflowrate();
                    } else if (millisPassed > 30000 && millisPassed <= 90000) {
                        statusIfDown();
                        time_of_stage = ((millisInFuture + 30000) - millisPassed) % 60000;
//                        progress3 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    } else if (millisPassed > 120000 && millisPassed <= 150000 || millisPassed > 180000 && millisPassed <= 210000) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 30000;
                        progress3 = (int) (time_of_stage*100 / 30000);
                        Log.println(Log.VERBOSE,"progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }
                }
                //key:60 sec ----> 01:00
                else if (millisInFuture == 480000) {
                    if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 60000;
                        Log.println(Log.VERBOSE,"TIME OF STAGE = "+time_of_stage);
                        upArrowAnimation();
                        setflowrate();

                    } else if ((millisPassed > 60000 && millisPassed <= 180000)) {
                        statusIfDown();
                        time_of_stage = (millisInFuture - (millisPassed+60000)) % 120000;    //CREDIT GOES TO BASHMOHNDES AMIN
                        Log.println(Log.VERBOSE,"TIME OF STAGE = "+time_of_stage);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();

                    } else if ((millisPassed > 240000 && millisPassed <= 300000) || (millisPassed > 360000 && millisPassed <= 420000)){
                        statusIfDown();
                        time_of_stage = (millisInFuture - millisPassed) % 60000;    //CREDIT GOES TO BASHMOHNDES AMIN
                        Log.println(Log.VERBOSE,"TIME OF STAGE = "+time_of_stage);
                        updateStageDownCount();
                        downArrowAnimation();
                        setflowrate();

                    }

                }

            }

            @Override
            public void onFinish() {
                Toast.makeText(tool1mode1.this,"Done",Toast.LENGTH_SHORT).show();
                avd2.stop(); avd3.stop();
                try {
                    pb.setProgress(100);
//                    pb2.setProgress(0); pb3.setProgress(0);
                    stage_timer.setVisibility(View.INVISIBLE);
                    rotateLoading.setTranslationY(60);
                    rotateLoading.stop();
//                    progressBar.setVisibility(View.INVISIBLE);
//                    progressBar2.setVisibility(View.INVISIBLE);
//                    progressBar.setProgress(0);
//                    progressBar2.setProgress(0);
//                    progressBar.setTranslationY(60);
//                    progressBar2.setTranslationY(60);
                    flow.setTranslationY(60);


                    animation1.cancel(); animation2.cancel();

                    Intent stoppump = new Intent(tool1mode1.this,stop_pump.class);
                    startActivity(stoppump);
                    //Vibration
                    if (Build.VERSION.SDK_INT >= 26) {
                        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneshot(150,VibrationEffect.DEFAULT_AMPLITUDE));
                    } else {
                        ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150},new int[]{VibrationEffect.EFFECT_CLICK},-1));
                    }
                }
                catch (NullPointerException e) {
                    e.printstacktrace();
                }

            }
        }.start();

这是倒计时结束的时间,另一个是在总时间内的不同阶段:

  private void stageSwitchVibration()
{
        Toast.makeText(tool1mode1.this,"stage switch",Toast.LENGTH_SHORT).show();
        //Vibration
        if (Build.VERSION.SDK_INT >= 26) {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createOneshot(300,VibrationEffect.DEFAULT_AMPLITUDE));
        } else {
            ((Vibrator) getSystemService(VIBRATOR_SERVICE)).vibrate(VibrationEffect.createWaveform(new long[]{150},-1));
        }

}

解决方法

您的 mCountDownTimer 是否与您的活动相关联?如果是这样,您可能需要在 mCountDownTimer.cancel(); 方法中调用 onDestroy(),即:

@Override
protected void onDestroy() {
    super.onDestroy();
    mCountDownTimer.cancel();
}
,

已注册的侦听器等与活动无关。如果您有活跃的侦听器,无论活动是否完成,他们都会保留对活动的引用。如果您不关闭所有侦听器,实际上会造成内存泄漏。

(生命周期感知组件除外,例如 ViewModel、LiveData)

这通常也可以通过正确实现一些模式(如 MVVM、MVC 等)并了解需要初始化/取消初始化哪些组件来避免。


从这里得到的主要结论是 Activity/Fragment/Service/etc. 中的任何代码。完成或停止时不会突然不再运行。

跟踪侦听器/回调或类似事件,并检查它们是否具有生命周期感知能力。如果没有,您需要自己取消注册。