Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
195 views
in Technique[技术] by (71.8m points)

How to completely end or kill an activity in android and transition to another?

I know this question was asked several times but I can't seem to make it work with any of the answers provided. I simply want with a click of a button to end the activity I am currently in and go to another activity. I want to delete it from the back stack as well, so I did the following in my Alert Dialog:

    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", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    //set what should happen when negative button is clicked
                    Toast.makeText(getApplicationContext(),"Cancelled",Toast.LENGTH_LONG).show();
                }
            })
            .show();
}

yet when I did go to the other activity after I thought I ended the one I was in, I was still getting things from the one that should have ended. I have vibrations and Toast messages that are triggered in the activity I was in they kept on coming even though it should have been over.

So what do I need to do? This app is a wearable app by the way just in case it works differently than on mobile phones

So in short I want to end the activity once and for all and clear it from the back stack.

Edit: regarding system services I do trigger vibrations at different times during my count down

        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","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,"CHECK","TIME OF STAGE = "+time_of_stage);
//                        Log.println(Log.VERBOSE,"CHECK","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","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,"CHECK","TIME OF STAGE = "+time_of_stage);
//                        Log.println(Log.VERBOSE,"CHECK","progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();

                        setFlowratetozero();

                    } else if ((millisPassed > 240000 && millisPassed <= 300000) || (millisPassed > 360000 && millisPassed <= 420000)){
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
//                        stepdownimage.setAnimation(animation);

                        Log.println(Log.ASSERT,"CHECK","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,"CHECK","TIME OF STAGE = "+time_of_stage);
//                        Log.println(Log.VERBOSE,"CHECK","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,"CHECK","TIME OF STAGE = "+time_of_stage);
//                        Log.println(Log.VERBOSE,"CHECK","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,"CHECK","TIME OF STAGE = "+time_of_stage);
                        Log.println(Log.VERBOSE,"CHECK","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,"CHECK","TIME OF STAGE = "+time_of_stage);
//                        Log.println(Log.VERBOSE,"CHECK","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,"CHECK","TIME OF STAGE = "+time_of_stage);
//                        Log.println(Log.VERBOSE,"CHECK","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,"CHECK","TIME OF STAGE = "+time_of_stage);
                        Log.println(Log.VERBOSE,"CHECK","progress3= "+progress3);
//                        pb3.setProgress(progress3);
                        updateStageDownCount();
                        downArrowAnimation();
                        setFlowratetozero();
                    }  else if (millisPassed > 480000 && millisPassed <= 600000 || mil

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Is your mCountDownTimer linked to your activity? if so, you may need to call mCountDownTimer.cancel(); in the onDestroy() method, ie:

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...