2017-07-03 100 views
1

我很新的Dagger2剛剛開始。我想實現這樣的目標,但沒有成功。Dagger2如何@Provide一種具有兩種不同的實現

這裏是我的模塊

@Module 
public class UtilModule 
{ 
    @Provides 
    @Named("fragmentUtilActivity") 
    public FragmentUtils providesFragmentUtilForActivity(Context context) 
    { 
     return new FragmentUtils(context); 
    } 

    @Provides 
    @Named("fragmentUtilFragment") 
    FragmentUtils providesFragmentUtilForFragment(Fragment fragment) 
    { 
     return new FragmentUtils(fragment); 
    } 

} 

這是我的組件

@Component(modules = UtilModule.class) 
public interface UtilComponent 
{ 
    @Named("fragmentUtilActivity") 
    FragmentUtils fragmentUtilsActivity(Context context); 

    @Named("fragmentUtilFragment") 
    FragmentUtils fragmentUtilsFragment(Fragment fragment); 
} 

這是我FragmentUtil

package myms.utils; 

import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 
import android.content.Context; 

import javax.inject.Inject; 

import myms.R; 

public class FragmentUtils 
{ 
    private Context context; 

    private Fragment hostFragment; 

    public FragmentUtils(Context context) 
    { 
     this.context = context; 
    } 

    public FragmentUtils(Fragment hostFragment) 
    { 
     this.hostFragment = hostFragment; 
    } 

    public void addFragment(Fragment fragment, boolean addToBackStack) 
    { 
     FragmentTransaction transaction = ((Activity) context).getFragmentManager() 
                   .beginTransaction(); 
     transaction.add(R.id.fragment_container, fragment, null); 

     if(addToBackStack) 
     { 
      transaction.addToBackStack(null); 
     } 

     transaction.commit(); 
    } 

    public void addNestedFragment(Fragment fragment, boolean addToBackStack) 
    { 
     FragmentTransaction transaction = hostFragment.getChildFragmentManager().beginTransaction(); 
     transaction.add(R.id.nested_fragment_container, fragment, null); 

     if(addToBackStack) 
     { 
      transaction.addToBackStack(null); 
     } 

     transaction.commit(); 
    } 

    public void replaceNestedFragment(Fragment fragment, boolean addToBackStack) 
    { 
     FragmentTransaction transaction = hostFragment.getChildFragmentManager().beginTransaction(); 
     transaction.replace(R.id.nested_fragment_container, fragment, null); 

     if(addToBackStack) 
     { 
      transaction.addToBackStack(null); 
     } 

     transaction.commit(); 
    } 
} 

我想是使用fragmentUtils實例有兩個不同的實現一個是針對活動和其他片段。請指導我做錯了什麼。

也可以有一個人請幫助我理解@Component接口無效注射(SomeClass的)的目的。

問候

+0

任何一個......? –

+0

你收到一個錯誤?請包括你面對的確切問題的描述。 –

+0

'FragmentUtils'看起來應該是2個不同的類別:一種是與主機片段的作品,以及一個與活動工作。你有2組方法,調用錯誤的方法會導致NullPointerException。這是一個強烈的信號,這個代碼應該被分成兩個不同的類。 –

回答

2

好努力之後我能夠通過修改我的UtilMoudle類

package myms.modules; 

import android.app.Fragment; 
import android.content.Context; 

import javax.inject.Named; 

import dagger.Module; 
import dagger.Provides; 
import myms.utils.FragmentUtils; 


@Module 
public class UtilModule 
{ 
    private Context context; 

    private Fragment fragment; 


    public UtilModule(Context context) 
    { 
     this.context = context; 
    } 

    public UtilModule(Fragment fragment) 
    { 
     this.fragment = fragment; 
    } 

    @Provides 
    @Named("fragmentUtilActivity") 
    public FragmentUtils providesFragmentUtilForActivity(Context context) 
    { 
     return new FragmentUtils(context); 
    } 

    @Provides 
    @Named("fragmentUtilFragment") 
    FragmentUtils providesFragmentUtilForFragment(Fragment fragment) 
    { 
     return new FragmentUtils(fragment); 
    } 

    @Provides 
    Context provideContext() 
    { 
     return context; 
    } 

    @Provides 
    Fragment provideFragment() 
    { 
     return fragment; 
    } 

} 

所以基本上我得爲我的方法,像我的情況下,上下文和片段提供的依賴關係來解決它。然後最後得到我必須這樣做的實例。例如對於活動

UtilComponent component = DaggerUtilComponent.builder() 
                .utilModule(new UtilModule(this)) 
                .build(); 
     FragmentUtils fragmentUtils = component.fragmentUtilsActivity(); 

請注意.utilModule(new UtilModule(this)),因爲這將在構建圖形時提供上下文。

我很新到這把匕首的事情,所以請謹慎使用這種方法。沒有保證/沒有索賠適用。快樂Daggering :)

相關問題