android - 如何在单击按钮时关闭活动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21473509/
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 - How to close an activity on button click?
提问by Arjun Krishnan
I want a button click to close an Activity. I am new to intents and and a little confused.
This is ActivityOne
which keeps a track of life cycle.
On a button press, it opens ActivityTwo
and puts ActivityOne
in background.
This works fine when I used this intent in my onClickListener
:
我想要一个按钮点击关闭一个活动。我对意图很陌生,而且有点困惑。这是ActivityOne
它跟踪生命周期。
按下按钮后,它会打开ActivityTwo
并置于ActivityOne
后台。
当我在我的中使用这个意图时,这很好用onClickListener
:
Intent myIntent = new Intent(ActivityOne.this, ActivityTwo.class);
ActivityOne.this.startActivity(myIntent);
In ActivityTwo
I need to use intents and finish()
method to close the activity. I tried to Google it and I thought this might work:
在ActivityTwo
我需要使用意图和finish()
方法来关闭活动。我尝试谷歌它,我认为这可能有效:
Intent myIntent2 = new Intent(getApplicationContext(), ActivityTwo.class);
ActivityTwo.this.finish(myIntent2);
It didn't work the full code is below:
它没有工作完整的代码如下:
public class ActivityOne extends Activity {
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_one);
// ...
Button launchActivityTwoButton = (Button) findViewById(R.id.bLaunchActivityTwo);
launchActivityTwoButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
// Launch Activity Two
// Hint: use Context's startActivity() method
// Create an intent stating which Activity you would like to start
Intent myIntent = new Intent(ActivityOne.this, ActivityTwo.class);
// Launch the Activity using the intent
ActivityOne.this.startActivity(myIntent);
}
});
// ...
}
}
Activity 2:
活动二:
public class ActivityTwo extends Activity {
// ...
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
// ...
Button closeButton = (Button) findViewById(R.id.bClose);
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
// This function closes Activity Two
// Hint: use Context's finish() method
Intent myIntent2 = new Intent(getApplicationContext(), ActivityTwo.class);
ActivityTwo.this.finish(myIntent2);
}
});
// ...
}
}
回答by Michael Yaworski
You want to close ActivityTwo
when a button is clicked? Just use finish();
您想在ActivityTwo
单击按钮时关闭吗?只需使用finish();
Full code for the button listener in ActivityTwo
would be:
按钮侦听器的完整代码ActivityTwo
是:
Button closeButton = (Button) findViewById(R.id.bClose);
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO:
// This function closes Activity Two
// Hint: use Context's finish() method
finish();
}
});
回答by Namrata
@Arjun Krishnan you want to kill activityTwo?
@Arjun Krishnan 你想杀死activityTwo吗?
try this
尝试这个
ActivityTwo.this.finish();
It would help you
它会帮助你
回答by SMR
Alright the best way to finish the current Activityis by using finish()
method. So inside the onClick()
of your button in the ActivityTwo
you can do this.
好吧,完成当前 Activity的最佳方法是使用finish()
方法。所以在onClick()
你的按钮里面ActivityTwo
你可以做到这一点。
closeButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
//closes ActivityTwo
}
});
no need to make new intents for finishing current Activity. Also note that pressing backwill also finish your Activity.
无需为完成当前 Activity 制定新的意图。另请注意,按回也将完成您的活动。
回答by Waqas
Replace these two lines
替换这两行
Intent myIntent2=new Intent(getApplicationContext(),ActivityTwo.class);
ActivityTwo.this.finish(myIntent2);
Intent myIntent2=new Intent(getApplicationContext(),ActivityTwo.class);
ActivityTwo.this.finish(myIntent2);
with this ActivityTwo.this.finish();
有了这个 ActivityTwo.this.finish();
回答by Pacific P. Regmi
Button exit= (Button) findViewById(R.id.yes);
exit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
MainActivity.this.finish();
}
});