制作一个重启按钮,Android应用游戏 [英] Making a restart button for an Android app game

查看:226
本文介绍了制作一个重启按钮,Android应用游戏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个Android应用程序,当我运行的主要活动的应用程序运行,我想它,但你在游戏中死亡后停止。我想要让你在游戏中死亡后重新启动按钮出现。到目前为止,我做了一个按钮和主要活动的背景的新布局。我在源文件夹做了一个新的类,并已成立了一个onclicklistener,但我不知道什么code将使该按钮启动的主要活动重新开始。

I am working on an android app and when I run the main activity the app runs as I want it to but stops after you die in the game. I want to make a restart button appear after you die in the game. So far I've made a new layout with a button and the background of the main activity. I made a new class in my source folder and have set up an onclicklistener but I'm not sure what code will make the the button start the main activity start again.

推荐答案

您可以添加以下code到你的onclick方法:

you can add the following code to your onclick method:

finish();
startActivity(getIntent());

中加入以下code,你重新启动相同的活动......没有要回previous之一。

by adding the following code, you are restarting the same activity... without going back to the previous one.

更新

在这里你可以如何变化,并重新启动游戏中,你需要添加标志重新开始活动。

here how you can change and restart the game, you need to add flags to start the activity over.

    Intent i = new Intent(this, MainMenu.class); //change it to your main class
    //the following 2 tags are for clearing the backStack and start fresh
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    finish();
    startActivity(i);

尝试一下,希望它为你的作品

try it out, hope it works for you

这篇关于制作一个重启按钮,Android应用游戏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆