由pressing后退按钮要回去片段 [英] Going back in Fragments by pressing the back button

查看:146
本文介绍了由pressing后退按钮要回去片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要回去另一个片段由pressing后退按钮。我已经读过,那 addToBackStack(字符串标签)应该帮助,但它并没有真正发挥作用。

I want to go back to another fragment by pressing the back button. I already read, that the addToBackStack (String tag) should help but it didn't really work.

下面是切换片段时,我在做什么。

Here is what I'm doing when switching fragments.

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(R.anim.slide_in_left, R.anim.slide_out_right);
ft.replace(R.id.content_frame, new MainFragment());
ft.addToBackStack("Mainfragment");
ft.commit();

所以,现在,该片段打开,并开始了的AsyncTask ,其中装载圈出现。加载后,被显示的数据。当我现在$一次p $ PSS后退按钮,片段可以追溯到的AsyncTask 在创建负载循环的开始。但的AsyncTask 不会继续。当我再次preSS后退按钮,应用程序关闭。

So now, the Fragment opens, and starts an AsyncTask where a loading circle appears. After the loading the data gets displayed. When I now press the back button once, the fragment goes back to the start of the AsyncTask where the loading circle is created. But the AsyncTask doesn't continue. When I press the back button again, the app closes.

我尝试添加 onBack pressed ,但它只是告诉我,这不会在一个片段工作。什么是去这里最好的方法是什么?

I tried to add onBackPressed but it just told me, that this won't work in a Fragment. What would be the best way to go here?

修改澄清: 有没有错误。它只是不工作。这就像我甚至不具备行addToBackStack -

Edit for clarification: There's no error. It's just not working. It is like I don't even have the line addToBackStack –

推荐答案

我收到了同样的情况,我结束了这个解决方案。当您添加或更换的片段,你需要把它添加到backStack一个独特的名字。然后,当pressed后退按钮就可以看到该片段是活跃的之一,低于您所创建的片段的FragmentActivity内的方法。

I had the same situation before and I ended up with this solution. When you add or replace the Fragments, you need to add it to the backStack with a unique name. Then when the back button is pressed you can see which fragment was the active one with the method below inside the FragmentActivity that you created the Fragment.

private String getCurrentFragmentName() {

    int backStackEntryCount = getSupportFragmentManager().getBackStackEntryCount();

    String fragmentName;

    if (backStackEntryCount > 0) {
        fragmentName = getSupportFragmentManager().getBackStackEntryAt(backStackEntryCount - 1).getName();
    } else {
        fragmentName = "";
    }

    return fragmentName;
}

和上的onkeydown()方法做到以下几点。

and in on onKeyDown() method do the following.

    if (keyCode == KeyEvent.KEYCODE_BACK && getCurrentFragmentName().equals("your fragment name")) {
        // Handle back press for this case.
        return true;

    } else if (keyCode == KeyEvent.KEYCODE_BACK
            && getCurrentFragmentName().equals("your another fragment")) {

        // Handle back press for another Fragment
        return true;

    } else {
        return super.onKeyDown(keyCode, event);
    }

这是地方如何我添加了片段与backStack

And this is the Place how I add the Fragment with backStack

transaction.addToBackStack("Your Fragment Name");

这篇关于由pressing后退按钮要回去片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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