2015-02-11 44 views
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(); 
     } 
    } 
} 

在一個新項目中,我開始使用匕首和嵌套片段,在我的腦海中出現了兩個問題。 1.應該將片段注入活動還是其他片段? 2.在配置更改後注入處理娛樂的碎片的正確方法是什麼? 我遇到的問題是在上面的代碼中,另一個HostedFragment將被創建並注入到HostActivity中。注入片段和配置更改處理

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

修改上面的版本可能避免創建重複HostedFragment但如果我們需要其他注射然後片段,它們不是娛樂時注入。

任何人都可以幫助我嗎?

回答

0

我想出了一個方法,我是否正確? 唯一困擾我的是,如果HostedFragment沒有在@Module注入中指定,我無法從圖中得到它。

公共類HostActivity擴展活動私有靜態最終字符串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); 
    } 
} 

}