如何关闭活动并返回到previous活动的android [英] How to close activity and go back to previous activity in android

查看:113
本文介绍了如何关闭活动并返回到previous活动的android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个主要活动,当我点击一个按钮,启动了新的活动,我用下面的code这样做的:

I have a main activity, that when I click on a button, starts a new activity, i used the following code to do so:

Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);

以上code从主要活动运行。

The above code was run from the main activity.

现在在我这就是所谓的主要活动新的活动,我有一个后退按钮。 当我点击这个后退按钮我希望我的新的活动,关闭,它必须回到原来的主要活动。

Now in my new activity which is called by the main activity, I have a back button. When I click on this back button I want my new activity to close and it must go back to the original main activity.

我已经打过电话 super.finish()和公正的完成()(由新活动),但这然后关闭我的整个应用程序(包括我的主要活动)。

I have tried calling super.finish() and just finish() (from the new activity) but this then closes my entire application (including my main activity).

如何才能关闭当前处于焦点的活动,然后返回到主活动呢?

How can I just close the activity that is currently in focus, and then return to the main activity?

编辑:

这是我的手机后退按钮也关闭了我的整个应用程序,使我认为,我已经开始了次要活动不正确的事实?

The fact that my phones back button also closes my entire app, leads me to think that i have started up the secondary activity incorrectly?

好,我一直在寻找,

我创建了一个使用相同的清单code和相同的code键启动活性的设置活动。

I created a Settings Activity that uses the same manifest code and the same code to Start the activity.

有关,当我按下后退按钮的设置活动,它返回到主活动。

For the settings Activity when I push the back button, it returns to the Main activity.

通过以​​上的主要问题,则退出我的整个应用程序中提到的活动。 所以,问题似乎并没有要与code来完成的活动,但活动自身。

With the activity mentioned above in the main Question it simply exits my entire app. So the problem doesn't seem to be with the code to finish the activity but the activity its self.

推荐答案

我想你调用 MainActivity 完成() $ C>当你开始你的 SettingsActivity

误或者将机器人:noHistory =真正的 MainActivity 在AndroidManifest.xml中

Mistakenly either you set the android:noHistory = "true" in MainActivity in AndroidManifest.xml.

在$ P $自动pssing返回键的活性完成

On pressing the back key the activity automatically finish.

在你切换到'SettingsActivity',你已经在你的MainActivity,这会导致pressing按钮返回到主屏幕,因为MainActivity已经被打死称为完成()。

Before you switch to your 'SettingsActivity', you have called finish() in your MainActivity, which causes pressing button go back to main screen since MainActivity was already killed.

这篇关于如何关闭活动并返回到previous活动的android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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