FragmentAdapter不成立的片段的很好的参考 [英] FragmentAdapter doesn't hold the good reference of the fragment

查看:140
本文介绍了FragmentAdapter不成立的片段的很好的参考的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在尝试与片段工作,但我坚持同一个问题,我解决不了。

I'm currently trying to work with fragment, but I'm stuck with an issue I can't solve.

我有一个活动,它拥有4种不同的片段。通过这次活动,我启动肚里了的AsyncTask到网络并获得不同的数据,我需要,然后将其发送到片段。

I have one activity, which holds 4 different fragment. From this activity, I launch an ASyncTask which goes to the web and get different data I need, and then will send it to the fragments.

但是,当我的应用程序杀死后再次打开,或当我改变方向,我的片段显然重新创建我的自定义FragmentAdapter未持有本很好的参考片段。

But, when my app gets killed and opened again, or when I change the orientation, my fragments are apparently recreated and my custom FragmentAdapter doesn't hold the good reference to the fragment.

下面是我的主要活动code。

Here is the code of my main activity.

public class MainActivity extends FragmentActivity {
    MyPagerAdapter fgsAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //...
        FragmentManager fm = super.getSupportFragmentManager();
        fgsAdapter = new MyPagerAdapter(fm,this);
        ViewPager myPager = (ViewPager) findViewById(R.id.home_pannels_pager);
        myPager.setAdapter(fgsAdapter);
        myPager.setCurrentItem(0);
    }
    @Override
    protected void onResume() {
        super.onResume();
        ATaskGetUser task = new ATaskGetUser(callback, (ProgressBar) findViewById(R.id.PB_AsyncTask));
        task.execute();
    }
    //What's called by the ASyncTask onPostExecute()
    private void notifyDataChanged() {
        fgsAdapter.notifyFragments(user.getItems());
    }

    private class MyPagerAdapter extends FragmentPagerAdapter {
        private List<CardFragment> fragments = new ArrayList<CardFragment>();
        private Context c;

        public MyPagerAdapter(FragmentManager fm, Context c) {
            super(fm);
            CardFragment h = new HabitFragment();
            CardFragment d = new DailyFragment();
            CardFragment t = new ToDoFragment();
            CardFragment r = new RewardFragment();
            fragments.add(h);
            fragments.add(d);
            fragments.add(t);
            fragments.add(r);
        }
        public int getCount() {
            return fragments.size();
        }

        @Override
        public CardFragment getItem(int position) {
            Log.v("MainActivity_fgsmanager", "getItem()");
            CardFragment f = (CardFragment) this.fragments.get(position);
            return f;
        }
        public void notifyFragments(List<HabitItem> items) {
            for(OnTasksChanged f : fragments) {
                f.onChange(items);
            }
        }
    }
}

所以,我希望能够做的,是能够调用的onChange(由我的四个片段实现的接口),在我的 notifyDataChanged 功能。这是有可能,我在想走错路了?

So, what I want to be able to do, is to be able to call the onChange (an interface implemented by my four fragments), in my notifyDataChanged function. Is this possible, are am I thinking the wrong way?

推荐答案

我得到了同样的问题,一旦与片段,我每一个屏幕旋转后丢失当前片段。

I got the same problems once with Fragments, I was losing the current fragment after every screen rotation.

我只需在片段类添加一行(而不是在父类FragmentActivity)解决了它

I simply solved it by adding one line in the Fragment class (not in the parent FragmentActivity class):

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.rate_fragment, container,false);

    setRetainInstance(true);//Added not to lose the fragment instance during screen rotations

(...)

有关你的情况下您的应用程序就会被杀死,并再次打开,我不知道它会工作虽然。

For your case where your app gets killed and opened again, I am not sure it will work though.

这篇关于FragmentAdapter不成立的片段的很好的参考的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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