0
public class HostActivity extends Activity {
    @Inject HostedFragment fragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_host);

        ObjectGraph.create(new HostActivityModule()).inject(this);

        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit();
        }
    }

    public static class HostedFragment extends Fragment{
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_hosted, container, false);
        }
    }

    @Module(injects = HostActivity.class)
    public static class HostActivityModule {
        @Provides @Singleton
        HostedFragment provideHostedFragment() {
            return new HostedFragment();
        }
    }
}

In a new project, I start to use dagger and nested fragment and come got a 2 questions in my mind. 1. Should fragment be injected into activity or another fragment? 2. What is the correct way to inject fragment which handle recreation after configuration change? The problem I encounter is in the above code, another HostedFragment will be created and injected into HostActivity.

if (savedInstanceState == null) {
   ObjectGraph.create(new HostActivityModule()).inject(this);
    getFragmentManager().beginTransaction().add(R.id.fragment_container, fragment).commit();
}

Modifying to the above version might avoid duplicate HostedFragment being created but if we need injections other then fragment, they are not injected during recreation.

Can anyone help me?

nhaarman
  • 98,571
  • 55
  • 246
  • 278
Morty Choi
  • 373
  • 2
  • 10

1 Answers1

0

I come up with an approach, am I correct? The only thing bother me is if HostedFragment is not specified in the @Module inject, I cannot get it from the graph.

public class HostActivity extends Activity { private static final String FRAGMENT_TAG = "fragment tag";

private ObjectGraph objectGraph;
private HostedFragment fragment;
@Inject LocationManager locationManager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_host);

    // Injecting fields that are required
    objectGraph = ObjectGraph.create(new HostActivityModule(this));
    objectGraph.inject(this);

    // Injecting fragment
    fragment = (HostedFragment) getFragmentManager().findFragmentByTag(FRAGMENT_TAG);
    if (fragment == null) {
        fragment = objectGraph.get(HostedFragment.class);
        getFragmentManager().beginTransaction().add(R.id.fragment_container, fragment, FRAGMENT_TAG).commit();
    }
}

public static class HostedFragment extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_hosted, container, false);
    }
}

@Module(
        injects = {
                HostActivity.class,
                HostedFragment.class
        },
        library = true
)
public static class HostActivityModule {

    private Context mContext;

    public HostActivityModule(Context mContext) {
        this.mContext = mContext;
    }

    @Provides @Singleton HostedFragment provideHostedFragment() {
        return new HostedFragment();
    }

    @Provides @Singleton LocationManager provideLocationManager() {
        return (LocationManager) mContext.getSystemService(LOCATION_SERVICE);
    }
}

}

Morty Choi
  • 373
  • 2
  • 10