按后退按钮返回 Fragments [英] Going back in Fragments by pressing the back button

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

问题描述

我想通过按后退按钮返回另一个片段.我已经读过,addToBackStack (String tag) 应该有帮助,但它并没有真正起作用.

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();

现在,Fragment 打开,并启动一个 AsyncTask,其中出现一个加载圆圈.加载后显示数据.当我现在按一次后退按钮时,片段返回到 AsyncTask 的开头,在那里创建加载圈.但是 AsyncTask 不会继续.当我再次按下后退按钮时,应用程序关闭.

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.

我尝试添加 onBackPressed 但它只是告诉我,这在 Fragment 中不起作用.去这里的最佳方式是什么?

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 –

推荐答案

我之前也遇到过同样的情况,最终得到了这个解决方案.添加或替换 Fragments 时,需要将其添加到具有唯一名称的 backStack.然后,当按下后退按钮时,您可以使用以下方法在您创建 Fragment 的 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 添加 Fragment 的地方

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

transaction.addToBackStack("Your Fragment Name");

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

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