Scopes Modules Components and Subcomponent in Dagger 2

Scopes

ApplicationScope.java

import javax.inject.Scope;
@Scope
public @interface ApplicationScope {
}

ActivityScope.java

import javax.inject.Scope;
@Scope
public @interface ActivityScope {
}

Modules

MyAppModule.java

@Module
public class MyAppModule {
    private final Context mAppContext;

    public MyAppModule(Application application) {
        mAppContext = application;
    }

    @Provides
    @ApplicationScope
    Context providesMyAppContext() {
        return mAppContext;
    }
}

PersistentModule.java

@Module
public class PersistentModule {
    @Provides
    @ApplicationScope
    SharedPreferences providesSharedPreferences(Context myAppContext) {
        return PreferenceManager.getDefaultSharedPreferences(myAppContext);
    }
}

ActivityModule.java

@Module
public class ActivityModule {
    private Activity activity;

    public ActivityModule(Activity activity) {
        this.activity = activity;
    }

    @Provides
    Activity activity() {
        return activity;
    }
}

Components

MyAppComponent.java

@ApplicationScope
@Component(modules = {MyAppModule.class, PersistentModule.class})
public interface MyAppComponent {
    ActivityComponent addChild(ActivityModule activityModule);
    void inject(MyApp myApp);
}

ActivityComponent.java

@ActivityScope
@Subcomponent(modules = {ActivityModule.class})
public interface ActivityComponent {
    void inject(MainActivity mainActivity);
}

Usages

MyApp.java

public class MyApp extends Application {

    private MyAppComponent mMyAppComponent;

    @Override
    public void onCreate() {
        super.onCreate();

        mMyAppComponent = DaggerMyAppComponent.builder()
                .myAppModule(new MyAppModule(this))
                .persistentModule(new PersistentModule())
                .webServiceModule(new WebServiceModule(Constants.BASE_URL))
                .build();
        mMyAppComponent.inject(this);
    }

    public MyAppComponent getMyAppComponent() {
        return mMyAppComponent;
    }
}

MainActivity.java

public class MainActivity extends AppCompatActivity {
    @Inject SharedPreferences sharedPreferences;

    private static final String TEXT_KEY = "text_key";

    private EditText edText;
    private TextView tvDisplay;

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

        ((MyApp) getApplication()).getMyAppComponent()
                .addChild(new ActivityModule(this))
                .inject(this);

        edText = (EditText) findViewById(R.id.ed_text);
        tvDisplay = (TextView) findViewById(R.id.tv_display);
    }

    public void save(View v) {
        String text = edText.getText().toString();
        SharedPreferences.Editor ed = sharedPreferences.edit();
        ed.putString(TEXT_KEY, text);
        boolean isSaved  = ed.commit();
        Snackbar.make(v, (isSaved) ? "Text Saved!" : "Failed to save the text!", Snackbar.LENGTH_SHORT).setAction("Action", null).show();
    }

    public void retrieve(View v) {
        String savedText = sharedPreferences.getString(TEXT_KEY, "");
        tvDisplay.setText(savedText);
    }

activity_main.xml




    
    
        

AndroidManifest.xml




    
        
            
                

                
            
        
    


Search within Codexpedia

Custom Search

Search the entire web

Custom Search