pressing的"去返回]按钮关闭应用程序 [英] Pressing the "go back" button closes the application

查看:131
本文介绍了pressing的"去返回]按钮关闭应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我正在慢慢移动,但在Android应用程序和Im的建设稳步前行目前正在学习如何创建一个新的窗口,并切换到它。这将会非常不错,但是我有一个小问题。当我pressing的返回按钮可关闭即使我已经选用回去的时候只是按钮pressed应用程序。

  @覆盖
公共无效onBack pressed(){
    完();
    返回;
}

有我错过了什么还是什么?

先谢谢了。

修改

  @覆盖
公共布尔onCreateOptionsMenu(菜单菜单)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main,菜单);
    返回true;
}@覆盖
公共布尔onOptionsItemSelected(菜单项项)
{
    //处理项目选择
    开关(item.getItemId())
    {
    案例R.id.menuItem1:
        的setContentView(R.layout.about);
        返回true;
    案例R.id.menuItem2:
        System.exit(0);
        返回true;
    默认:
        返回super.onOptionsItemSelected(项目);
    }
}

编辑2 :About.java

 包weather.right;进口weather.right.now.R;
进口android.os.Bundle;公共接口关于{
    公共无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.about);
    }}


解决方案

您需要使用意图来切换窗口。 窗口是使用<$ C名为 Android中的活动并设置了活动的可视内容活动的onCreate()调用$ C>的setContentView()方法。

要推出另一活动从当前的活动,只需创建一个意图有几个参数,并调用 startActivity()意图。这里有一个例子:

 意向书I =新意图(这一点,TheNextActivity.class);
startActivity(ⅰ);

不要忘了,包括在Android清单文件的第二个活动。所有活动在应用程序中必须包含在该文件中。

其他的事情需要注意的是,你真的不使用 System.exit() Android中。只需拨打完成()。它的建议,让Android的管理应用程序和它的资源,而不是自己做,但如果你想确保你的应用程序确实已关闭,随意使用 System.exit()反正。但也没有必要重写 onBack pressed()如果你只叫完成()。这是Android的标准的行为,当你点击后退按钮。

此外,你不叫的setContentView()不是每个活动一次。你开始一个新的活动当你需要改变视觉效果(或使用专门的窗口小部件之一,布局之间切换。

这也解释了为什么您遇到的问题。您可能已经更改了活动使用的setContentView()的布局,但仍然只有一个活动运行 - 当你调用完成(),即活动被关闭。如果你已经开始了第二次活动有不同的布局,就像你应该做的,Android的将关闭的第二个活动键,将返回到第一个。

I'm currently moving slowly but steady forward in the making of a Android application and Im currently learning how to create a new window and switch to it. This is going all well but I have one small problem. When I pressing the "go back" button closes the application even if I have choosed to go back when just that button is pressed.

@Override
public void onBackPressed() {
    finish();
    return;
}

Have I missed something or what?

Thanks in advance.

EDIT

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    MenuInflater menuInflater = getMenuInflater();
    menuInflater.inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item)
{       
    // Handle item selection
    switch (item.getItemId())
    {
    case R.id.menuItem1:
        setContentView(R.layout.about);
        return true;
    case R.id.menuItem2:
        System.exit(0);
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}

EDIT 2: About.java

package weather.right;

import weather.right.now.R;
import android.os.Bundle;

public interface About {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.about);
    }

}

解决方案

You need to use Intents to "switch windows". A "window" is called an Activity in Android and you set the visual content of an Activity using the setContentView() method in the onCreate() call of the Activity.

To launch another Activity from your current Activity, simply create an Intent with a few parameters and call startActivity() with that Intent. Here's an example:

Intent i = new Intent(this, TheNextActivity.class);
startActivity(i);

Don't forget to include your second Activity in the Android manifest file. All Activities in your application must be included in that file.

Other things to note is that you don't really use System.exit() in Android. Just call finish(). It's advised to let Android manage applications and its resources rather than doing it yourself, but if you want to make sure that your application really is shut down, feel free to use System.exit() anyway. There's also no need for overriding onBackPressed() if you're only calling finish(). That's standard behaviour in Android when you hit the back button.

Also, you don't call setContentView() more than once per Activity. You start a new Activity when you need to change the visuals (or use one of the specialized Widgets to switch between layouts.

This also explains why you're experiencing your "problem". You may have changed the layout of the Activity using setContentView(), but there's still only one Activity running - when you call finish(), that Activity gets closed. If you had started a second Activity with a different layout, like you're supposed to do, Android would have closed that second Activity and would have returned you to the first.

这篇关于pressing的&QUOT;去返回]按钮关闭应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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