[英]how to return to previous activity from about with a button click?
Im trying to return from the about dialog to the main activity by a button click: 我试图通过单击按钮从“关于”对话框返回到主活动:
public class AboutActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.about);
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
SharedPreferences prefs = getSharedPreferences("com.example.tiocontas",MODE_PRIVATE);
SharedPreferences.Editor prefsEditor = prefs.edit();
prefsEditor.putBoolean("FirstTime", false);
finish();
//this.onBackPressed();
}
}
I've tried both finish() and onBackPressed() with no results, im doing something wrong could someone give me some hints? 我尝试了finish()和onBackPressed()都没有结果,即时消息做错了什么,有人可以给我一些提示吗?
From what I can see in your code, you don't have a button attached to your onClick()
method. 从您的代码中可以看到,您的
onClick()
方法没有附加按钮。 You can do this two ways, in xml or programmatically. 您可以通过xml或以编程方式实现这两种方式。
In xml 在xml中
<Button
...
android:onClick="functionName"/>
Then in your code, define your function which you named in your xml 然后在您的代码中,定义您在xml中命名的函数
public void functionName(View v)
{
// some code
finish();
}
Programmatically, declare your button 以编程方式声明您的按钮
Button aBtn = (Button) findViewById(R.id.button_id);
aBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// some code
AboutActivity.this.finish()
}
});
If you have already attached your button to the onClick()
in some way not shown then you may be finishing your main activity. 如果您已经以某种未显示的方式将按钮附加到
onClick()
,那么您可能正在完成主要活动。 In which case, describe what happens when you click the button and show your Main Activity 在这种情况下,请描述单击按钮并显示主要活动时发生的情况
It seems like you have already finished; 看来您已经完成了; your previous Activity.
您之前的活动。 Remove finish() from MainActivity where you got to your aboutActiivty.
从MainActivity除去aboutActiivty处的finish()。
You can call onBackPressed() function on btnclick and then can define the following code at end in your class: 您可以在btnclick上调用onBackPressed()函数,然后可以在类末尾定义以下代码:
@Override
public void onBackPressed() {
super.onBackPressed();
Intent intent = new Intent(this, YourActivity.class);
startActivity(intent);
}
hope this will help you. 希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.