Lars
Lars

Reputation: 574

How to load data into a FragmentActivity to be used by nested fragments?

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

Upvotes: 0

Views: 180

Answers (1)

Marcin Orlowski
Marcin Orlowski

Reputation: 75629

You do this wrong. You should NOT pause anything. Instead, when parent activity load what is needed it should tell the fragments about that. In fact I'd make it differently - by using listeners - so my fragment would need to register on creation and the object that loads my data would then broadcast message back once loading task it done.

Upvotes: 3

Related Questions