在低内存杀Viewpager宽松的片段连接 [英] Viewpager loose fragment connection on low memory kill

查看:213
本文介绍了在低内存杀Viewpager宽松的片段连接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动我使用viewpager。我创建了两个片段,并连接到视图寻呼机和不时打电话从这个片段在我的活动有一定的方法.. AFER应用程序被杀害在低内存和重建碎片存在,但与viewpager somehome没有任何联系。我可以不叫MyFragment.mymethod()..它说MyFragment为空

 公共类SectionsPagerAdapter扩展FragmentPagerAdapter {        公共SectionsPagerAdapter(FragmentManager FM){
            超(FM);
        }        @覆盖
        公共片段的getItem(INT位置){
            //的getItem被称为实例化片段为给定的页面。
            如果(位置== 0){
                如果(homeFragment == NULL){
                    homeFragment =新HomeFragment();
                }
                返回homeFragment;
            }其他{
                如果(mapFragment == NULL){
                    mapFragment =新地图();
                }
                返回mapFragment;
            }
        }



  @覆盖
    公共布尔onOptionsItemSelected(菜单项项){
        INT ID = item.getItemId();
        如果(ID == R.id.action_search){
            返回true;
        }否则如果(ID == R.id.action_feedback){        }否则如果(ID == R.id.action_rate_me){        }否则如果(ID == R.id.action_show_favorites){
            如果(mViewPager.getCurrentItem()!= 0){
                mViewPager.setCurrentItem(0);
            }
            homeFragment.populateListWithFavorites();
            返回true;        }否则如果(ID == R.id.action_history){
            如果(mViewPager.getCurrentItem()!= 0){
                mViewPager.setCurrentItem(0);
            }
            homeFragment.populateListWithHistory();
            返回true;
        }        返回super.onOptionsItemSelected(项目);
    }


解决方案

我找到了解决办法......

这是真的就像一个说,在主要活动我的片段变量被清除,所以我不得不碎片没有联系了..我所做的是在里面onAttach()我联系我的片段在MainActivity变量再次

  @覆盖
    公共无效onAttach(活动活动){
        super.onAttach(活动);
        mActivity =活动;
        ((RestaurantActivityViewPager)mActivity).menuFragment =这一点;
    }
    公共静态的newInstance MenuFragment(INT sectionNumber){
        MenuFragment片段=新MenuFragment();
        捆绑ARGS =新包();
        args.putInt(ARG_SECTION_NUMBER,sectionNumber);
        fragment.setArguments(参数);
        返回片段;
    }

in my activity i use viewpager. I create two fragments and attach the to the view pager and time to time calling some methods from this fragments in my activity.. afer the app is killed on low memory and recreated fragments are there but no connection with the viewpager somehome.. i can not call MyFragment.mymethod().. it says MyFragment is null

 public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given page.
            if(position==0){
                if(homeFragment==null){
                    homeFragment=new HomeFragment();
                }
                return homeFragment;
            }else{
                if(mapFragment==null){
                    mapFragment=new Map();
                }
                return mapFragment;
            }
        }

. . .

 @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if(id==R.id.action_search){
            return true;
        }else if(id==R.id.action_feedback){

        }else if(id==R.id.action_rate_me){

        }else if(id==R.id.action_show_favorites ){
            if(mViewPager.getCurrentItem()!=0){
                mViewPager.setCurrentItem(0);
            }
            homeFragment.populateListWithFavorites();
            return true;

        }else if(id==R.id.action_history){
            if(mViewPager.getCurrentItem()!=0){
                mViewPager.setCurrentItem(0);
            }
            homeFragment.populateListWithHistory();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

解决方案

I found the solution....

it is really like a said that my fragment variables in the main activity is cleared so i have no connection to the fragments anymore.. what i did is inside the onAttach() i linked my fragment to the mainActivity variables again

@Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        mActivity = activity;
        ((RestaurantActivityViewPager)mActivity).menuFragment=this;
    }
    public static MenuFragment newInstance(int sectionNumber) {
        MenuFragment fragment = new MenuFragment();
        Bundle args = new Bundle();
        args.putInt(ARG_SECTION_NUMBER, sectionNumber);
        fragment.setArguments(args);
        return fragment;
    }

这篇关于在低内存杀Viewpager宽松的片段连接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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