Android - 内容更改后内容消失

时间:2014-03-22 10:07:57

标签: android android-orientation

我的SectionsPageAdapter看起来像这样:

public class SectionsPagerAdapter extends FragmentPagerAdapter {
    private List<PopularFragment> fragments;

    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
        fragments = new ArrayList<MainActivity.PopularFragment>();
    }

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a PlaceholderFragment (defined as a static inner class
        // below).
        PopularFragment f = PopularFragment.newInstance(position);
        fragments.add(f);

        return f;
    }

    public List<PopularFragment> getFragments() {
        return fragments;
    }
(...)

Activity onCreate我致电:

new DownloadTask(new Handler()) {
    private boolean result;

    @Override
    void doOnUiThread() {
        if (!result) {
            MainActivity.noConnection(MainActivity.this);
            return;
        }

        for (PopularFragment f : mSectionsPagerAdapter.getFragments())
            f.load();

        loaded = true;
    }

    @Override
    void doInBackground() {
        result = popular.download();
    }
}.execute();

在方向更改后,永远不会调用load()。这是为什么?

以下是整个MainActivity.javahttp://pastebin.com/0pEDdjHS

1 个答案:

答案 0 :(得分:1)

可能只是因为在适配器中的getItem之前调用了doOnUiThread。 检查mSectionsPagerAdapter.getFragments()它可能是空的