简体   繁体   English

按下 android 手机的后退按钮时如何停止/取消可运行?

[英]How to stop/cancel a runnable when pressing on android phone's back button?

In my project, I have an activity called "ExamMenuActivity" where I can choose between "Addition, Subtraction, Multiplication and Division" activities.在我的项目中,我有一个名为“ExamMenuActivity”的活动,我可以在“加法、减法、乘法和除法”活动之间进行选择。

In Addition activity (called ExamAdditionActivity) I have a handler method to regenerate the question form after a given answer.在 Addition 活动(称为 ExamAdditionActivity)中,我有一个处理程序方法可以在给定答案后重新生成问题表单。 Everything seems working fine, I can generate the question and give an answer and after giving an answer, a new question is generated in 2 seconds.一切似乎都很好,我可以生成问题并给出答案,给出答案后,2 秒内就会生成一个新问题。

The issue I am having is, after I give a correct or a wrong answer to the question, within 2 seconds, if I quickly press on the back button of the phone and don't wait for the question to regenerate, I come back to the Exam Menu page as I wanted (back to ExamMenuActivity) but the screen changes back to ExamAdditionActivity and I see a new generated question again.我遇到的问题是,在我对问题给出正确或错误的答案后,在 2 秒内,如果我快速按下手机的后退按钮并且不等待问题重新生成,我会回到我想要的考试菜单页面(返回到 ExamMenuActivity),但屏幕变回 ExamAdditionActivity 并且我再次看到一个新生成的问题。

So I want to be able to come back to Exam Menu activity again when I press on the back button of the phone before the question regenerates and I don't want to face back the ExamAdditionActivity again with a new generated question (say I changed my mind after giving an answer to an addition question and I wanted to choose another activity from the menu and I didn't wait for at least 2 seconds).因此,当我在问题重新生成之前按下手机的后退按钮时,我希望能够再次回到考试菜单活动,并且我不想用新生成的问题再次面对 ExamAdditionActivity(比如我改变了我的在回答一个附加问题后介意,我想从菜单中选择另一个活动,我没有等待至少 2 秒)。 I have tried overriding the activity with onBackPressed method:我尝试使用 onBackPressed 方法覆盖活动:

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

but unfortunately that didn't work.但不幸的是,这没有用。

Here is how I regenerate the question.这是我如何重新生成问题。 I basically restart the same activity in two seconds with a handler ( there was a runnable within my handler code but since it was showing anonymous, android studio offered me to change it to a lambda function):我基本上用一个处理程序在两秒钟内重新启动相同的活动(我的处理程序代码中有一个可运行但由于它显示匿名,android 工作室让我将其更改为 lambda 函数):

private void regenerateQuestion() {
        new Handler().postDelayed(() -> {
            Intent restartExamAdditionActivity = new Intent(ExamAdditionActivity.this, ExamAdditionActivity.class);

            overridePendingTransition(0, 0);
            startActivity(restartExamAdditionActivity);
            finish();
            overridePendingTransition(0, 0);
        }, TIME_OUT);

    }

Even though I am not sure but I think I am having the problem in button listeners area since I give an answer to the question and call regenetate() method there under each button.尽管我不确定,但我认为我在按钮侦听器区域遇到了问题,因为我给出了问题的答案并在每个按钮下调用 regenetate() 方法。

I hope there can be an answer to my issue.我希望我的问题能有答案。 Thank you so much for stopping by to check on my post!非常感谢您过来查看我的帖子!

Create your Handler as a member variable of your class, like this:创建您的Handler作为 class 的成员变量,如下所示:

Handler myHandler = new Handler();

Create your Runnable as a member variable of your class, like this:创建Runnable作为 class 的成员变量,如下所示:

Runnable myRunnable = new Runnable() {
    @Override
    public void run() {
        Intent restartExamAdditionActivity = new Intent(ExamAdditionActivity.this, ExamAdditionActivity.class);

        overridePendingTransition(0, 0);
        startActivity(restartExamAdditionActivity);
        finish();
        overridePendingTransition(0, 0);
    }
};

Post the Runnable using the new variable:使用新变量发布Runnable

myHandler.postDelayed(myRunnable, TIME_OUT);

When you want to cancel the regeneration, do this:如果要取消重新生成,请执行以下操作:

myHandler.removeCallbacks(myRunnable);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 当按下键盘上的某个按钮时如何停止程序,例如s - How to stop the program when pressing a certain button on the keyboard, for example s 按下片段中的设备后退按钮时如何使媒体播放器停止返回活动 - How to make media player stop when pressing device back button from a fragment back to activity 如果在取消时运行正在进行中,如何取消ShceduledFuture并等待runnable停止? - How to cancel ShceduledFuture and wait for runnable to stop, if runnable is in progress at the moment of cancellation? 在Android上,如果按下后退按钮,可运行线程会如何处理? - On Android what happens to Runnable thread when back button pressed? 使用python在Android手机上按下按钮 - Pressing button on android phone with python 当按下后退按钮时,如何停止android中的asynctask? - How can I stop asynctask in android when back button is pressed? 如何在使用postDelayed()循环的Android中停止Runnable? - How to stop Runnable in Android that's looping using postDelayed()? 如何设置一个视图,当用户单击电话的后退按钮时,它将返回到同一java类中的视图 - how to set a view that when a user click phone's back button, it back to the view in same java class 按下手机上的“后退”按钮可以改变意图 - Pressing back button on phone has different behaviour to back intent 按下返回按钮时如何获取最后使用的片段 - How to get last Fragment used when pressing back button
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM