避免重装在片段pssed回$ P $ [英] avoid reloading on back pressed in fragments

查看:163
本文介绍了避免重装在片段pssed回$ P $的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做汽车finding.I应用程序正在显示结果GridView和也对右手边滑动菜单(搜索过滤器)。我使这个整个应用程序与fragments.In的帮助下,我的GridView有一个负载更多的页脚底部,当我点击第12位的GridView,我开始另一个片段是这样的:

I am making an app for car finding.I am showing result in gridview and also have a sliding menu on right hand side(for search filter).I have made this whole app with the help of fragments.In the gridview I have a load more footer at bottom, when I click on 12th position gridview , I am starting another fragments like this:

SearchDetailActivity.goToFragment(ProductDetailFragment.newInstance(map,position));

和在ProductDetailFragment我有一个返回按钮,回去就这样previous片段:

and in ProductDetailFragment I have a back button,to go back on previous fragment like this:

SearchDetailActivity.goToFragment(SearchDetailFragment.newInstance(str_url));

现在,当我再次回来的数据从0-10th项目展示。

Now when I came back again data shows from 0-10th item.

在goToFragment()我写这篇code:

In goToFragment() I have write this code:

public static void goToFragment(Fragment fragment) {
    Log.d("GoToFrag","sjdk>>"+fragment);
    Fragment tmp = fm.findFragmentByTag(fragment.getClass().getName());
    if (tmp != null && tmp.isVisible())
        return;

    ft = fm.beginTransaction();
    ft.replace(R.id.main_fragment, fragment, fragment.getClass().getName());
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.commit();
    currentTag = fragment.getClass().getName();
}

我想避免在装载数据来自ProductDetailFragment.If回来时,我用的活动然后我可以使用onBack pressed()来备份和重装避免,但片段时,我pressed应用程序的后退按钮,它重装在SearchDetatilFragment,这是非常annoying..Please帮助me..Thanks提前。

I want to avoid data reload when come back from ProductDetailFragment.If I used Activity then I can use onBackPressed() to back and avoid reloading but in Fragment when I pressed app's back button ,It reload the SearchDetatilFragment,which is very annoying..Please help me..Thanks in advance.

推荐答案

我无意中发现了这个问题,约3个月。最后我的努力得到了回报,并找到了解决方案。

I have stumbled on this issue for about 3 months. Finally my efforts were paid off and found a solution.

问题是,当你的用户替换,这相当于删除和添加。
所以片段完全去除并重新创建时,后退按钮是pssed $ P $。
相反,我们必须隐藏父片段,并显示了孩子。

The problem is, when you user "replace", this is equivalent to "remove and add". so the fragment is completely removed and recreated when back button is pressed. instead we have to hide the parent fragment and show the child.

在哪里检索算法片段被称为

Where serach fragment is called

 ft = fm.beginTransaction();
 ft.replace(R.id.main_fragment, yourSearchFragment, "searchFragment");
 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 ft.commit();

当调用产品详细片段

 ft = fm.beginTransaction();
 ft.hide(getFragmentManager().findFragmentByTag("searchFragment"));
 ft.add(R.id.main_fragment, yourDetailfragment);
 ft.addToBackStack(null);
 ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 ft.commit();

好运气

这篇关于避免重装在片段pssed回$ P $的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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