Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support new onAttach(Context) on Fragment's lifecycle #64

Merged
merged 3 commits into from
Nov 8, 2016
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
Expand All @@ -13,6 +14,9 @@ public class DefaultFragmentLightCycle<T extends Fragment> implements FragmentLi
@Override
public void onAttach(T fragment, Activity activity) { /* no-op */ }

@Override
public void onAttach(T fragment, Context context) { /* no-op */ }

@Override
public void onCreate(T fragment, Bundle bundle) { /* no-op */ }

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
package com.soundcloud.lightcycle;

import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public interface FragmentLightCycle<T extends Fragment> {
void onAttach(T fragment, Activity activity);
@TargetApi(23) void onAttach(T fragment, Context context);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you break the line after @TargetApi(23), please ?

Not your fault, we did not setup a proper code formatter and checker.
#65

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, Fixed!

void onCreate(T fragment, Bundle bundle);
void onViewCreated(T fragment, View view, Bundle savedInstanceState);
void onActivityCreated(T fragment, Bundle bundle);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.soundcloud.lightcycle;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
Expand Down Expand Up @@ -105,6 +108,7 @@ public void onDestroy(Target activity) {
};
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static <Source extends android.app.Fragment, Target extends Source> FragmentLightCycle<Target> lift(final FragmentLightCycle<Source> lightCycle) {
return new FragmentLightCycle<Target>() {

Expand All @@ -113,6 +117,11 @@ public void onAttach(Target fragment, Activity activity) {
lightCycle.onAttach(fragment, activity);
}

@Override
public void onAttach(Target fragment, Context context) {
lightCycle.onAttach(fragment, context);
}

@Override
public void onCreate(Target fragment, Bundle bundle) {
lightCycle.onCreate(fragment, bundle);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
Expand Down Expand Up @@ -36,6 +37,13 @@ public void onAttach(T fragment, Activity activity) {
}
}

@Override
public void onAttach(T fragment, Context context) {
for (FragmentLightCycle<T> component : fragmentLightCycles) {
component.onAttach(fragment, context);
}
}

@Override
public void onCreate(T fragment, @Nullable Bundle bundle) {
for (FragmentLightCycle<T> component : fragmentLightCycles) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Build;
import android.os.Bundle;
import android.view.MenuItem;
Expand All @@ -27,6 +28,19 @@ public void bind(FragmentLightCycle<FragmentType> lifeCycleComponent) {
}

@Override
@TargetApi(23)
public void onAttach(Context context) {
super.onAttach(context);
bindIfNecessary();
lifeCycleDispatcher.onAttach(fragment(), context);
}

/*
* Deprecated on API 23
* Use onAttach(Context) instead
*/
@Override
@SuppressWarnings("deprecation")
public void onAttach(Activity activity) {
super.onAttach(activity);
bindIfNecessary();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.view.View;

Expand All @@ -20,6 +21,7 @@ public class FragmentLightCycleDispatcherTest {
@Mock private FragmentLightCycle<Fragment> lifeCycleComponent2;
@Mock private Fragment fragment;
@Mock private Activity activity;
@Mock private Context context;
private FragmentLightCycleDispatcher<Fragment> dispatcher;

@Before
Expand All @@ -37,6 +39,14 @@ public void shouldNotifyOnAttach() {
verify(lifeCycleComponent2).onAttach(fragment, activity);
}

@Test
public void shouldNotifyOnAttachAPI23() {
dispatcher.onAttach(fragment, context);

verify(lifeCycleComponent1).onAttach(fragment, context);
verify(lifeCycleComponent2).onAttach(fragment, context);
}

@Test
public void shouldNotifyOnCreate() {
final Bundle bundle = Bundle.EMPTY;
Expand Down