Java Android:从类中实现后退按钮

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18614663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 09:31:45  来源:igfitidea点击:

Android: Implementing a Back Button from a class

javaandroidbuttonback

提问by Ignasi Sánchez

Okay, so here is my problem. I'm working on an android app and learning android at the same time, so most of the times I get errors. Normaly I can fix them after researching a bit, but I'm getting stuck in this point.

好的,这是我的问题。我正在开发一个 android 应用程序并同时学习 android,所以大多数时候我都会出错。通常我可以在研究一下后修复它们,但我陷入了这一点。

I'm trying to make a back button for every Activity in my app, so I thought about making a "BackButton" class, so I can instanciate it every time I want to. Here is my BackButton code:

我正在尝试为我的应用程序中的每个 Activity 制作一个后退按钮,所以我想制作一个“BackButton”类,这样我每次都可以实例化它。这是我的后退按钮代码:

import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.app.Activity;

    public class BackButton extends Activity implements View.OnClickListener{

        public static Button BackButton;

        // Defining the button
        public BackButton() {

            BackButton = (Button) findViewById(R.id.bBack);

            BackButton.setOnClickListener(this);

        }

        //To get the Button
        public static Button getBackButton() {
            return BackButton;
        }


        // OnClickListener
        public void onClick(View v) {

                try {
                    Class MainActivityClass = Class.forName("eu.lafarga.treballderecerca.MainActivity");
                    Intent MainActivityIntent = new Intent(BackButton.this, MainActivityClass); 
                    startActivity(MainActivityIntent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }finally {
                    // Save the things we've done. 
                }

        }

    }

So, how should I implement this in any activity? I'm doind something wrong? (Sure I'm lol)

那么,我应该如何在任何活动中实现这一点?我做错了什么?(当然我是哈哈)

采纳答案by codeMagic

Personally, I would suggest not doing this. I think overriding the back button in each Activitywould be safer, more flexible, and just as easy, if not easier. Chances are, you aren't always going to want to return to the MainActivityas your app grows because its likely that this won't be the expected action for the users when they hit the back button. Override the back button in Activitiesthat need it and run your code

就个人而言,我建议不要这样做。我认为覆盖每个中的后退按钮Activity会更安全、更灵活,而且同样简单,如果不是更容易的话。MainActivity很有可能,随着应用程序的增长,您并不总是想要返回到 ,因为这可能不是用户点击后退按钮时的预期操作。覆盖Activities需要它的后退按钮并运行您的代码

    @Overrride
    public void onBackPressed(View v) {
     // save data first

      Intent MainActivityIntent = new Intent(CurrentActivityName.this, MainActivityClass); 
      startActivity(MainActivityIntent);
      super.onBackPressed();
}

You can also use flags such as FLAG_ACTIVITY_CLEAR_TOPif you want to remove all Activitiesbetween the current one and the target Activity(here MainActivity) by calling setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

您还可以使用标志,例如,FLAG_ACTIVITY_CLEAR_TOP如果您想通过调用删除Activities当前和目标Activity(此处MainActivity)之间的所有内容setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)

回答by JavaDM

There is a easier way to Listen to the backButton.

有一种更简单的方法来收听 backButton。

// Called when the activity has detected the user's press of the back key.
 onBackPressed()

And when you do not call the super-method, there shouldn't be any further steps. But it is not a good way to "disable" the back-button.

当您不调用超级方法时,就不应该有任何进一步的步骤。但这不是“禁用”后退按钮的好方法。

http://developer.android.com/reference/android/app/Activity.html

http://developer.android.com/reference/android/app/Activity.html

回答by Rinkal Bhanderi

There are multiple ways to block back button.

有多种方法可以阻止后退按钮。

By overriding onKeyDownor onBackPressedof your Activity.

通过覆盖onKeyDownonBackPressed您的Activity.

Please visit following question link for detailed description and its solution given in answer.

请访问以下问题链接以获取详细说明及其答案中给出的解决方案。

Link 1

链接 1

Link 2

链接 2