当后退按钮pressed Android应用程序关闭 [英] Android app closes when back button pressed

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

问题描述

我有以下的活动会启动时选择了选项卡的片段:

I have the following activity which launches a fragment when the tab is selected:

public class MainActivity extends Activity implements TabListener {
    Fragment f = null;
.....

    public void onTabSelected(Tab tab, FragmentTransaction ft) {

        .....

        if (tab.getPosition() == 0) {
            if (initalSync == true) {
                progress1.setVisibility(TRIM_MEMORY_UI_HIDDEN);
            }
            f = new EventFragment();
            Bundle data = new Bundle();
            data.putInt("idx", tab.getPosition());
            f.setArguments(data);

        }
        if (tab.getPosition() == 1) {
            progress1.setVisibility(TRIM_MEMORY_UI_HIDDEN);
            f = new MapsFragment();
            Bundle data = new Bundle();
            data.putInt("idx", tab.getPosition());
            f.setArguments(data);

        }
        .....    
        ft.replace(android.R.id.content, f);

    }

在以往I $上的任何片段的对$ PSS手机后退按钮会关闭我的应用程序。我知道这是关系到backstack,但是每一个方法我都试过失败。

When ever I press the phones back button on any of the fragments it closes my app. I know this is related to the backstack but every method I have tried fails.

什么想法?

推荐答案

您需要调用<一个href="http://developer.android.com/reference/android/app/FragmentTransaction.html#addToBackStack%28java.lang.String%29"相对=nofollow> addToBackstack(空)的交易的片段添加到backstack。然后后退按钮应该恢复到previous片段。

You need to call addToBackstack(null) on the Transaction to add the fragment to the backstack. Then the back button should revert to the previous fragment.

这篇关于当后退按钮pressed Android应用程序关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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