Last active
February 8, 2016 16:59
-
-
Save benoitjadinon/3f1932163c65b4c31be0 to your computer and use it in GitHub Desktop.
Android : Roboguice base controller class containing lifecycle methods ready to override, to avoid explicitly using lifecycle events @observes everywhere
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package roboutils; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.content.Intent; | |
| import android.content.res.Configuration; | |
| import android.os.Bundle; | |
| import android.view.View; | |
| import com.google.inject.Inject; | |
| import roboguice.activity.event.OnActivityResultEvent; | |
| import roboguice.activity.event.OnContentChangedEvent; | |
| import roboguice.activity.event.OnNewIntentEvent; | |
| import roboguice.activity.event.OnPauseEvent; | |
| import roboguice.activity.event.OnRestartEvent; | |
| import roboguice.activity.event.OnResumeEvent; | |
| import roboguice.activity.event.OnStopEvent; | |
| import roboguice.context.event.OnConfigurationChangedEvent; | |
| import roboguice.context.event.OnCreateEvent; | |
| import roboguice.context.event.OnDestroyEvent; | |
| import roboguice.context.event.OnStartEvent; | |
| import roboguice.event.Observes; | |
| public abstract class AbstractRoboController | |
| { | |
| @Inject | |
| Activity mActivity; | |
| //region LOLCycle | |
| @SuppressWarnings("unused") | |
| private void onNewIntentEvent (@Observes OnNewIntentEvent event) { | |
| onNewIntent(event.getActivity()); | |
| } | |
| protected void onNewIntent(Activity activity) {} | |
| @SuppressWarnings("unused") | |
| private void onCreate (@Observes OnCreateEvent event) { | |
| onCreateActivity(event.getContext(), event.getSavedInstanceState()); | |
| } | |
| protected void onCreateActivity(Context context, Bundle savedInstanceState) { } | |
| @SuppressWarnings("unused") | |
| protected void onContentChanged (@Observes OnContentChangedEvent event) { | |
| onContentChanged(event.getActivity()); | |
| } | |
| private void onContentChanged(Activity activity) { | |
| onCreated(mActivity, mActivity.findViewById(android.R.id.content)); | |
| } | |
| protected void onCreated(Activity activity, View rootView){} | |
| @SuppressWarnings("unused") | |
| private void onRestartEvent (@Observes OnRestartEvent event) { | |
| onRestart(event.getActivity()); | |
| } | |
| protected void onRestart(Activity activity) {} | |
| @SuppressWarnings("unused") | |
| private void onStartEvent (@Observes OnStartEvent event) { | |
| onStart(event.getContext()); | |
| } | |
| protected void onStart(Context context) {} | |
| @SuppressWarnings("unused") | |
| private void onResumeEvent (@Observes OnResumeEvent event) { | |
| onResume(event.getActivity()); | |
| } | |
| protected void onResume(Activity activity) {} | |
| @SuppressWarnings("unused") | |
| private void onConfigurationChanged (@Observes OnConfigurationChangedEvent event) { | |
| onConfigurationChanged(event.getContext(), event.getNewConfig(), event.getOldConfig()); | |
| } | |
| protected void onConfigurationChanged(Context context, Configuration newConfig, Configuration oldConfig) {} | |
| @SuppressWarnings("unused") | |
| private void onPauseEvent (@Observes OnPauseEvent event) { | |
| onPause(event.getActivity()); | |
| } | |
| protected void onPause(Activity activity) {} | |
| @SuppressWarnings("unused") | |
| private void onStopEvent (@Observes OnStopEvent event) { | |
| onStop(event.getActivity()); | |
| } | |
| protected void onStop(Activity activity) {} | |
| @SuppressWarnings("unused") | |
| private void onDestroy (@Observes OnDestroyEvent event) { | |
| onDestroy(event.getContext()); | |
| } | |
| protected void onDestroy(Context context) { } | |
| @SuppressWarnings("unused") | |
| private void onActivityResult (@Observes OnActivityResultEvent event) { | |
| onActivityResult(event.getActivity(), event.getRequestCode(), event.getResultCode(), event.getData()); | |
| } | |
| protected void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {} | |
| //endregion LOLCycle | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment