0

I have a FragmentActivity with two Fragments. The two Fragments depend on a shared object. The object is loaded by an AsyncTask. So the problem here is that when the activity creates and shows up the fragments, the object is not loaded yet. Is there any method that pauses the Fragment creation or something like this?

The scenario is like this:

FragmentActivity

public class MainActivity extends FragmentActivity {

    private Object sharedObject;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        new FooAsyncTask(...).execute();
    }

    ...

    public void onAsyncTaskCompleted(result) {
        sharedObject = result;
    }

    // Pager adapter -------------------------------------------------------
    private class SectionsPagerAdapter extends FragmentPagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            switch(position) {
                case 0:
                    return FooFragment.newInstance(sharedObject);
                case 1:
                    return BarFragment.newInstance(sharedObject);
                default:
                    throw new IllegalStateException();
            }
        }

        @Override
        public String getPageTitle(int position) {
            switch(position) {
                case 0:
                    return "Foo";
                case 1:
                    return "Bar";
                default:
                    throw new IllegalStateException();              
            }
        }

        @Override
        public int getCount() {
            return 2;
        }

    }
}

FooFragment

public class FooFragment extends Fragment {
    public FooFragment newInstance(Object sharedObject) {
        FooFragment f = new FooFragment();
        f.setObject(sharedObject);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...
        // Here I use the object to show data
    }
}

BarFragment

public class BarFragment extends Fragment {
    public BarFragment newInstance(Object sharedObject) {
        BarFragment f = new BarFragment();
        f.setObject(sharedObject);
        return f;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ...
    }

    ...

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ...
        // Here I use the object to show data
    }
}

Final thoughts

So, as you can see, I need the same data for the two fragments, and this data needs to be loaded in an AsyncTask. The ideal behavior for me would be:

  1. Load MainActivity
  2. Show ProgressDialog and load the data in the background
  3. Dismiss ProgressDialog
  4. Show Fragments and use the data
4

1 回答 1

3

你这样做是错误的。你不应该暂停任何事情。相反,当父活动加载所需的内容时,它应该告诉片段。事实上,我会做不同的事情——通过使用监听器——所以我的片段需要在创建时注册,加载我的数据的对象将在加载任务完成后广播回消息。

于 2013-07-05T12:43:00.783 回答