Java tutorial
/* * Copyright (C) 2015 Sergio Martinez Open Source Project * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package me.martinez.sergio.daggertwobasearchitecture.activities; import android.os.Bundle; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.Button; import java.util.List; import javax.inject.Inject; import me.martinez.sergio.daggertwobasearchitecture.di.components.DaggerSectionComponentMain; import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseFragment; import me.martinez.sergio.daggertwobasearchitecture.di.injectableelements.base.BaseInjectionActivity; import me.martinez.sergio.daggertwobasearchitecture.di.modules.ActivityModule; import me.martinez.sergio.daggertwobasearchitecture.fragments.MainFragment; import me.martinez.sergio.daggertwobasearchitecture.R; import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.firststep.FirstFragment; import me.martinez.sergio.daggertwobasearchitecture.fragments.sections.testsection.secondstep.SecondFragment; import me.martinez.sergio.daggertwobasearchitecture.test.A; import me.martinez.sergio.daggertwobasearchitecture.test.B; import me.martinez.sergio.daggertwobasearchitecture.utils.Log4Me; public class MainActivity extends BaseInjectionActivity<MainActivityComponent> implements View.OnClickListener { private Button buttonClearFragments; private Button buttonAddFirstFragment; private Button buttonAddSecondFragment; @Inject A a; @Inject B b; @Inject Log4Me logger; @Inject List<String> diInjectionHistory; private Object sectionComponent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initButtons(); initFragment(); initOnClickListeners(); testDI(); } private void initButtons() { buttonAddSecondFragment = (Button) findViewById(R.id.buttonAddFirstFragment); buttonAddFirstFragment = (Button) findViewById(R.id.buttonAddSecondFragment); buttonClearFragments = (Button) findViewById(R.id.buttonClearFragments); } private void initOnClickListeners() { buttonAddSecondFragment.setOnClickListener(this); buttonAddFirstFragment.setOnClickListener(this); buttonClearFragments.setOnClickListener(this); } //region Init methods private void initFragment() { FragmentManager fragmentManager = getSupportFragmentManager(); MainFragment mainFragment = new MainFragment(); FragmentTransaction ft = fragmentManager.beginTransaction(); ft.replace(R.id.fragmentContainer, mainFragment); ft.commit(); } private void addFragment(BaseFragment baseFragment) { FragmentManager fragmentManager = getSupportFragmentManager(); FragmentTransaction ft = fragmentManager.beginTransaction(); ft.add(R.id.fragmentContainer, baseFragment).addToBackStack(baseFragment.getClass().getSimpleName()) .commit(); } private void clearFragmentStack() { FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.popBackStack(FirstFragment.class.getSimpleName(), FragmentManager.POP_BACK_STACK_INCLUSIVE); } //endregion //region Android AutoGenerated methods @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } //endregion //region Test DI stuff private void testDI() { diInjectionHistory.add(MainActivity.class.getName()); diInjectionHistory.add(logger.toString()); diInjectionHistory.add(a.toString()); diInjectionHistory.add(b.toString()); } //endregion //region dependency injection Methods @Override protected void initDI() { activityComponent = DaggerMainActivityComponent.builder().appComponent(getAppComponent()) .activityModule(new ActivityModule(this)).mainActivityModule(new MainActivityModule(this)).build(); activityComponent.injectActivity(this); } //region Section Stuff private void initSectionComponent() { sectionComponent = DaggerSectionComponentMain.builder().mainActivityComponent(activityComponent).build(); } private void clearSectionComponent() { sectionComponent = null; } @Override public Object getActivityComponent() { if (sectionComponent != null) { return sectionComponent; } else { return activityComponent; } } //endregion //endregion @Override public void onClick(View v) { switch (v.getId()) { case R.id.buttonAddFirstFragment: initSectionComponent(); addFragment(new FirstFragment()); break; case R.id.buttonAddSecondFragment: addFragment(new SecondFragment()); break; case R.id.buttonClearFragments: clearSectionComponent(); clearFragmentStack(); break; default: break; } } }