如何返回到起始活性 [英] How to return to the starting activity
本文介绍了如何返回到起始活性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我编程一个简单的游戏,玩后,玩家看到一个得分屏幕,在其中点被写入。现在,我希望玩家有一个返回到主屏幕按钮,带来了他的主要活动,这是 StartActivity
。
我尝试以下code(见下文),但它不工作。我究竟做错了什么?
公共类ScoreActivity扩展活动实现OnClickListener {
@覆盖
保护无效的onCreate(包savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.activity_score);
Button按钮=(按钮)findViewById(R.id.startbutton);
button.setOnClickListener(本);
}
@覆盖
公共无效的onClick(视图v){
意向意图=新的意图(ScoreActivity.this,StartActivity.class);
组件名CN = intent.getComponent();
意图mainIntent = IntentCompat.makeRestartActivityTask(CN);
startActivity(mainIntent);
}
}
解决方案
添加以下标志
打电话时意图
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
所以这将删除previous回栈即现在你用这个。
@覆盖
公共无效的onClick(视图v){
意图mIntent =新的意图(ScoreActivity.this,StartActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(mIntent);
}
现在你是在活动开始时preSS当年的应用程序退出
I'm programming a simple game, and after playing, the player sees a score-screen, in which the points are written. Now, I want the player to have a "Return to main screen" button that brings him to the main activity, which is StartActivity
.
I tried the following code (see below), but it doesn't work. What am I doing wrong?
public class ScoreActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_score);
Button button = (Button)findViewById (R.id.startbutton);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
Intent intent = new Intent(ScoreActivity.this, StartActivity.class);
ComponentName cn = intent.getComponent();
Intent mainIntent = IntentCompat.makeRestartActivityTask(cn);
startActivity(mainIntent);
}
}
解决方案
Add following flags
when calling intent
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
so this will remove previous back stack i e now you use this .
@Override
public void onClick(View v) {
Intent mIntent = new Intent(ScoreActivity.this, StartActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(mIntent);
}
Now you are at starting activity and when press back then app exits
这篇关于如何返回到起始活性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文