Skip to content

Commit

Permalink
Inject Activities before calling super.onCreate()
Browse files Browse the repository at this point in the history
Fragments are attached in super.onCreate(), where they get injected, and therefore need the activity to have already been injected.

Fixes #598

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=149038239
  • Loading branch information
ronshapiro committed Mar 2, 2017
1 parent f5447bc commit 756456a
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion java/dagger/android/DaggerActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public abstract class DaggerActivity extends Activity implements HasDispatchingF

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/android/DaggerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public abstract class DaggerFragment extends Fragment implements HasDispatchingF

@Override
public void onAttach(Context context) {
super.onAttach(context);
AndroidInjection.inject(this);
super.onAttach(context);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/android/support/DaggerAppCompatActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ public abstract class DaggerAppCompatActivity extends AppCompatActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AndroidInjection.inject(this);
super.onCreate(savedInstanceState);
}

@Override
Expand Down
2 changes: 1 addition & 1 deletion java/dagger/android/support/DaggerFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public abstract class DaggerFragment extends Fragment implements

@Override
public void onAttach(Context context) {
super.onAttach(context);
AndroidSupportInjection.inject(this);
super.onAttach(context);
}

@Override
Expand Down
22 changes: 21 additions & 1 deletion javatests/dagger/android/support/functional/InjectorsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,25 +18,30 @@

import static com.google.common.truth.Truth.assertThat;

import android.content.res.Configuration;
import org.robolectric.RobolectricTestRunner;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.Robolectric;
import org.robolectric.android.controller.ActivityController;
import org.robolectric.annotation.Config;

@RunWith(RobolectricTestRunner.class)
public class InjectorsTest {
private static final String MANIFEST =
"//javatests/dagger/android/support/functional"
+ ":functional/AndroidManifest.xml";

private ActivityController<TestActivity> activityController;
private TestActivity activity;
private TestParentFragment parentFragment;
private TestChildFragment childFragment;

@Before
public void setUp() {
activity = Robolectric.setupActivity(TestActivity.class);
activityController = Robolectric.buildActivity(TestActivity.class);
activity = activityController.setup().get();
parentFragment =
(TestParentFragment)
activity.getSupportFragmentManager().findFragmentByTag("parent-fragment");
Expand Down Expand Up @@ -72,6 +77,8 @@ public void componentStructureFollowsControllerStructure() {
.ActivitySubcomponent.ParentFragmentSubcomponent.class,
ComponentStructureFollowsControllerStructureApplication.ApplicationComponent
.ActivitySubcomponent.ParentFragmentSubcomponent.ChildFragmentSubcomponent.class);

changeConfiguration();
}

@Test
Expand All @@ -92,5 +99,18 @@ public void AllControllersAreDirectChildrenOfApplication() {
AllControllersAreDirectChildrenOfApplication.ApplicationComponent.class,
AllControllersAreDirectChildrenOfApplication.ApplicationComponent
.ChildFragmentSubcomponent.class);

changeConfiguration();
}

// https://github.com/google/dagger/issues/598
private void changeConfiguration() {
Configuration oldConfiguration = activity.getResources().getConfiguration();
Configuration newConfiguration = new Configuration(oldConfiguration);
newConfiguration.orientation =
oldConfiguration.orientation == Configuration.ORIENTATION_LANDSCAPE
? Configuration.ORIENTATION_PORTRAIT
: Configuration.ORIENTATION_LANDSCAPE;
activityController.configurationChange(newConfiguration);
}
}

0 comments on commit 756456a

Please sign in to comment.