-
Notifications
You must be signed in to change notification settings - Fork 46
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
B: 2 - Migrated sample project view bindings to butterknife. Closes #11 #28
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,10 +19,15 @@ | |
import android.app.Fragment; | ||
import android.app.FragmentManager; | ||
import android.content.Context; | ||
import android.os.Bundle; | ||
import android.support.annotation.Nullable; | ||
import android.view.View; | ||
|
||
import javax.inject.Inject; | ||
import javax.inject.Named; | ||
|
||
import butterknife.ButterKnife; | ||
import butterknife.Unbinder; | ||
import dagger.android.AndroidInjection; | ||
import dagger.android.AndroidInjector; | ||
import dagger.android.DispatchingAndroidInjector; | ||
|
@@ -53,12 +58,51 @@ public abstract class BaseFragment extends Fragment implements HasFragmentInject | |
@Inject | ||
DispatchingAndroidInjector<Fragment> fragmentInjector; | ||
|
||
@Nullable | ||
private Unbinder viewUnbinder; | ||
|
||
@Override | ||
public void onAttach(Context context) { | ||
AndroidInjection.inject(this); | ||
super.onAttach(context); | ||
} | ||
|
||
@Override | ||
public void onViewStateRestored(Bundle savedInstanceState) { | ||
super.onViewStateRestored(savedInstanceState); | ||
View view = getView(); | ||
if (view != null) { | ||
/* | ||
* Bind the views here instead of in onViewCreated so that view state changed listeners | ||
* are not invoked automatically without user interaction. | ||
* | ||
* If we bind before this method (e.g. onViewCreated), then any checked changed | ||
* listeners bound by ButterKnife will be invoked during fragment recreation (since | ||
* Android itself saves and restores the views' states. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing closing parenthesis. |
||
* | ||
* The lifecycle order is as follows (same if added via xml or java or if retain | ||
* instance is true): | ||
* | ||
* onAttach | ||
* onCreateView | ||
* onViewCreated | ||
* onActivityCreated | ||
* onViewStateRestored | ||
* onResume | ||
*/ | ||
viewUnbinder = ButterKnife.bind(this, getView()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use the view instead of calling getView() again. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't be There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes. I didn't include |
||
} | ||
} | ||
|
||
@Override | ||
public void onDestroyView() { | ||
if (viewUnbinder != null) { | ||
viewUnbinder.unbind(); | ||
} | ||
super.onDestroyView(); | ||
} | ||
|
||
|
||
@Override | ||
public final AndroidInjector<Fragment> fragmentInjector() { | ||
return fragmentInjector; | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Null check not needed since
onViewStateRestored
only gets called when theView
returned inonCreateView
is not null. See 7370d99