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
Android: Implementing a Back Button from a class
提问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 Activity
would be safer, more flexible, and just as easy, if not easier. Chances are, you aren't always going to want to return to the MainActivity
as 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 Activities
that 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_TOP
if you want to remove all Activities
between 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