├── .gitignore ├── README.md ├── app ├── .gitignore ├── build.gradle ├── proguard-rules.pro └── src │ ├── main │ ├── AndroidManifest.xml │ ├── java │ │ └── de │ │ │ └── czyrux │ │ │ └── mvploadersample │ │ │ ├── SampleActivity.java │ │ │ ├── SampleFragment.java │ │ │ ├── SampleViewPagerFragment.java │ │ │ ├── base │ │ │ ├── BasePresenterActivity.java │ │ │ ├── BasePresenterFragment.java │ │ │ ├── Presenter.java │ │ │ ├── PresenterFactory.java │ │ │ └── PresenterLoader.java │ │ │ └── presenter │ │ │ ├── SamplePresenter.java │ │ │ ├── SamplePresenterFactory.java │ │ │ └── SampleView.java │ └── res │ │ ├── layout │ │ ├── activity_mvp.xml │ │ ├── fragment_pager.xml │ │ └── fragment_single.xml │ │ ├── menu │ │ └── activity_menu.xml │ │ ├── mipmap-hdpi │ │ └── ic_launcher.png │ │ ├── mipmap-mdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxhdpi │ │ └── ic_launcher.png │ │ ├── mipmap-xxxhdpi │ │ └── ic_launcher.png │ │ ├── values-w820dp │ │ └── dimens.xml │ │ └── values │ │ ├── colors.xml │ │ ├── dimens.xml │ │ ├── strings.xml │ │ └── styles.xml │ └── test │ └── java │ └── de │ └── czyrux │ └── mvploadersample │ └── ExampleUnitTest.java ├── build.gradle ├── gradle.properties ├── gradlew ├── gradlew.bat └── settings.gradle /.gitignore: -------------------------------------------------------------------------------- 1 | # Intellj 2 | /.idea/ 3 | *.iml 4 | 5 | # Mac OsX 6 | .DS_Store 7 | 8 | # Built application files 9 | *.apk 10 | *.ap_ 11 | 12 | # Files for the Dalvik VM 13 | *.dex 14 | 15 | # Java class files 16 | *.class 17 | 18 | # Generated files 19 | bin/ 20 | gen/ 21 | 22 | # Gradle files 23 | .gradle/ 24 | gradle/ 25 | build/ 26 | /*/build/ 27 | 28 | # Local configuration file (sdk path, etc) 29 | local.properties 30 | 31 | # Proguard folder generated by Eclipse 32 | proguard/ 33 | 34 | # Log Files 35 | *.log 36 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | MVP Loader 2 | ======= 3 | 4 | Sample project to showcase how to use Android [Loaders](https://developer.android.com/intl/es/guide/components/loaders.html) in the [Model View Presenter](https://en.wikipedia.org/wiki/Model%E2%80%93view%E2%80%93presenter) pattern to preserve a _Presenter_ across configuration changes. 5 | 6 | For a full explanation with this approach check this post: [Presenter surviving orientation changes with Loaders](https://medium.com/@czyrux/presenter-surviving-orientation-changes-with-loaders-6da6d86ffbbf#.epqrv6jqm) 7 | 8 | 9 | ## Version 10 | 1.0.0 11 | 12 | -------------------------------------------------------------------------------- /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply plugin: 'com.android.application' 2 | 3 | android { 4 | compileSdkVersion 24 5 | buildToolsVersion "24.0.2" 6 | 7 | defaultConfig { 8 | applicationId "de.czyrux.mvploadersample" 9 | minSdkVersion 14 10 | targetSdkVersion 24 11 | versionCode 1 12 | versionName "1.0" 13 | jackOptions { 14 | enabled true 15 | } 16 | } 17 | buildTypes { 18 | release { 19 | minifyEnabled false 20 | proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' 21 | } 22 | } 23 | 24 | compileOptions { 25 | sourceCompatibility JavaVersion.VERSION_1_8 26 | targetCompatibility JavaVersion.VERSION_1_8 27 | } 28 | } 29 | 30 | dependencies { 31 | compile fileTree(dir: 'libs', include: ['*.jar']) 32 | 33 | final SUPPORT_LIBRARY_VERSION = '24.2.1' 34 | 35 | final MOCKITO_VERSION = '1.10.19' 36 | final JUNIT_VERSION = '4.12' 37 | 38 | // ---- APP DEPENDENCIES 39 | 40 | compile "com.android.support:appcompat-v7:$SUPPORT_LIBRARY_VERSION" 41 | compile "com.android.support:design:$SUPPORT_LIBRARY_VERSION" 42 | 43 | 44 | // ---- UNIT TEST CONFIGURATION 45 | testCompile "org.mockito:mockito-core:$MOCKITO_VERSION" 46 | testCompile "junit:junit:$JUNIT_VERSION" 47 | } 48 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # By default, the flags in this file are appended to flags specified 3 | # in /Applications/adt_bundle/sdk/tools/proguard/proguard-android.txt 4 | # You can edit the include path and order by changing the proguardFiles 5 | # directive in build.gradle. 6 | # 7 | # For more details, see 8 | # http://developer.android.com/guide/developing/tools/proguard.html 9 | 10 | # Add any project specific keep options here: 11 | 12 | # If your project uses WebView with JS, uncomment the following 13 | # and specify the fully qualified class name to the JavaScript interface 14 | # class: 15 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 16 | # public *; 17 | #} 18 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/SampleActivity.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample; 2 | 3 | import android.content.Intent; 4 | import android.os.Bundle; 5 | import android.support.annotation.NonNull; 6 | import android.support.v4.app.Fragment; 7 | import android.util.Log; 8 | import android.view.Menu; 9 | import android.view.MenuItem; 10 | import android.widget.Toast; 11 | 12 | import de.czyrux.mvploadersample.base.BasePresenterActivity; 13 | import de.czyrux.mvploadersample.base.PresenterFactory; 14 | import de.czyrux.mvploadersample.presenter.SamplePresenter; 15 | import de.czyrux.mvploadersample.presenter.SamplePresenterFactory; 16 | import de.czyrux.mvploadersample.presenter.SampleView; 17 | 18 | public class SampleActivity extends BasePresenterActivity implements SampleView { 19 | private static final String TAG = "SampleActivity"; 20 | private static final int NUMBER_OF_PAGES = 10; 21 | 22 | @Override 23 | protected void onCreate(Bundle savedInstanceState) { 24 | super.onCreate(savedInstanceState); 25 | setContentView(R.layout.activity_mvp); 26 | Log.e(TAG, "onCreate-" + tag()); 27 | 28 | if (savedInstanceState == null) { 29 | getSupportFragmentManager() 30 | .beginTransaction() 31 | .replace(R.id.fragment_container, SampleFragment.newInstance("fragment", R.color.yellow)) 32 | .commit(); 33 | } 34 | } 35 | 36 | @Override 37 | protected void onStart() { 38 | super.onStart(); 39 | Log.e(TAG, "onStart-" + tag()); 40 | } 41 | 42 | @NonNull 43 | @Override 44 | protected String tag() { 45 | return "activity"; 46 | } 47 | 48 | @NonNull 49 | @Override 50 | protected PresenterFactory getPresenterFactory() { 51 | return new SamplePresenterFactory(tag()); 52 | } 53 | 54 | @Override 55 | protected void onPresenterCreatedOrRestored(@NonNull SamplePresenter presenter) { 56 | // Nothing right now 57 | } 58 | 59 | @Override 60 | public boolean onCreateOptionsMenu(Menu menu) { 61 | getMenuInflater().inflate(R.menu.activity_menu, menu); 62 | return true; 63 | } 64 | 65 | @Override 66 | public boolean onOptionsItemSelected(MenuItem item) { 67 | 68 | int id = item.getItemId(); 69 | if (id == R.id.menu_single) { 70 | replaceFragment(SampleFragment.newInstance("fragment", R.color.green)); 71 | return true; 72 | } else if (id == R.id.menu_pager) { 73 | replaceFragment(SampleViewPagerFragment.newInstance(NUMBER_OF_PAGES)); 74 | return true; 75 | } else if (id == R.id.menu_activity) { 76 | startActivity(new Intent(this, SampleActivity.class)); 77 | } 78 | 79 | return super.onOptionsItemSelected(item); 80 | } 81 | 82 | private void replaceFragment(Fragment fragment) { 83 | getSupportFragmentManager() 84 | .beginTransaction() 85 | .addToBackStack(null) 86 | .replace(R.id.fragment_container, fragment) 87 | .commit(); 88 | } 89 | 90 | @Override 91 | public void showMessage(String text) { 92 | Toast.makeText(this, text, Toast.LENGTH_SHORT).show(); 93 | } 94 | } 95 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/SampleFragment.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.ColorRes; 5 | import android.support.annotation.NonNull; 6 | import android.util.Log; 7 | import android.view.LayoutInflater; 8 | import android.view.View; 9 | import android.view.ViewGroup; 10 | import android.widget.TextView; 11 | 12 | import de.czyrux.mvploadersample.base.BasePresenterFragment; 13 | import de.czyrux.mvploadersample.base.PresenterFactory; 14 | import de.czyrux.mvploadersample.presenter.SamplePresenter; 15 | import de.czyrux.mvploadersample.presenter.SamplePresenterFactory; 16 | import de.czyrux.mvploadersample.presenter.SampleView; 17 | 18 | public class SampleFragment extends BasePresenterFragment implements SampleView { 19 | private static final String TAG = "SampleFragment"; 20 | private static final String ARG_NAME = "name"; 21 | private static final String ARG_COLOR = "color"; 22 | 23 | @ColorRes 24 | private int color; 25 | private String name; 26 | 27 | private TextView titleTextView; 28 | private SamplePresenter presenter; 29 | 30 | public static SampleFragment newInstance(String name, @ColorRes int color) { 31 | Bundle args = new Bundle(); 32 | args.putString(ARG_NAME, name); 33 | args.putInt(ARG_COLOR, color); 34 | 35 | SampleFragment fragment = new SampleFragment(); 36 | fragment.setArguments(args); 37 | return fragment; 38 | } 39 | 40 | 41 | @Override 42 | public void onCreate(Bundle savedInstanceState) { 43 | super.onCreate(savedInstanceState); 44 | if (getArguments() != null) { 45 | name = getArguments().getString(ARG_NAME); 46 | color = getArguments().getInt(ARG_COLOR); 47 | } 48 | 49 | Log.e(TAG, "onCreate-" + name); 50 | } 51 | 52 | @Override 53 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 54 | Bundle savedInstanceState) { 55 | View view = inflater.inflate(R.layout.fragment_single, container, false); 56 | view.setBackgroundColor(getResources().getColor(color)); 57 | 58 | titleTextView = (TextView) view.findViewById(R.id.single_text); 59 | return view; 60 | } 61 | 62 | @Override 63 | public void onActivityCreated(Bundle savedInstanceState) { 64 | super.onActivityCreated(savedInstanceState); 65 | Log.d(TAG, "onActivityCreated-" + tag()); 66 | } 67 | 68 | @Override 69 | public void onStart() { 70 | super.onStart(); 71 | Log.d(TAG, "onStart-" + tag()); 72 | // When first created the Fragment, the Presenter will be initialized at this point, but on 73 | // a configuration change it wont be ready until onResume 74 | Log.d(TAG, "onStart- is_presenter_null:" + String.valueOf(presenter == null)); 75 | } 76 | 77 | @Override 78 | public void onResume() { 79 | super.onResume(); 80 | Log.d(TAG, "onResume-" + tag()); 81 | Log.d(TAG, "onResume- is_presenter_null:" + String.valueOf(presenter == null)); 82 | } 83 | 84 | @Override 85 | public void onPause() { 86 | super.onPause(); 87 | Log.d(TAG, "onPause-" + tag()); 88 | 89 | } 90 | 91 | @Override 92 | public void onStop() { 93 | super.onStop(); 94 | Log.d(TAG, "onStop-" + tag()); 95 | } 96 | 97 | @NonNull 98 | @Override 99 | protected String tag() { 100 | return name; 101 | } 102 | 103 | @NonNull 104 | @Override 105 | protected PresenterFactory getPresenterFactory() { 106 | return new SamplePresenterFactory(name); 107 | } 108 | 109 | @Override 110 | protected void onPresenterCreatedOrRestored(@NonNull SamplePresenter presenter) { 111 | this.presenter = presenter; 112 | Log.d(TAG, "onPresenterCreatedOrRestored-" + tag()); 113 | } 114 | 115 | @Override 116 | public void showMessage(String text) { 117 | titleTextView.setText(text); 118 | } 119 | } 120 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/SampleViewPagerFragment.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample; 2 | 3 | import android.os.Bundle; 4 | import android.support.v4.app.Fragment; 5 | import android.support.v4.app.FragmentManager; 6 | import android.support.v4.app.FragmentStatePagerAdapter; 7 | import android.support.v4.view.ViewPager; 8 | import android.view.LayoutInflater; 9 | import android.view.View; 10 | import android.view.ViewGroup; 11 | 12 | public class SampleViewPagerFragment extends Fragment { 13 | private static final String ARG_PAGES = "pages"; 14 | private static final int OFF_SCREEN_LIMIT = 1; 15 | 16 | private int pages; 17 | private ViewPager viewPager; 18 | 19 | public static SampleViewPagerFragment newInstance(int pages) { 20 | Bundle args = new Bundle(); 21 | args.putInt(ARG_PAGES, pages); 22 | 23 | SampleViewPagerFragment fragment = new SampleViewPagerFragment(); 24 | fragment.setArguments(args); 25 | return fragment; 26 | } 27 | 28 | @Override 29 | public void onCreate(Bundle savedInstanceState) { 30 | super.onCreate(savedInstanceState); 31 | if (getArguments() != null) { 32 | pages = getArguments().getInt(ARG_PAGES); 33 | } 34 | } 35 | 36 | @Override 37 | public View onCreateView(LayoutInflater inflater, ViewGroup container, 38 | Bundle savedInstanceState) { 39 | View view = inflater.inflate(R.layout.fragment_pager, container, false); 40 | viewPager = (ViewPager) view.findViewById(R.id.viewpager); 41 | 42 | return view; 43 | } 44 | 45 | @Override 46 | public void onViewCreated(View view, Bundle savedInstanceState) { 47 | super.onViewCreated(view, savedInstanceState); 48 | viewPager.setAdapter(new PagerAdapter(getChildFragmentManager(), pages)); 49 | viewPager.setOffscreenPageLimit(OFF_SCREEN_LIMIT); 50 | } 51 | 52 | 53 | private static class PagerAdapter extends FragmentStatePagerAdapter { 54 | 55 | private static int[] COLOR_ARRAY = new int[]{R.color.blue, R.color.yellow, R.color.brown, R.color.pink, 56 | R.color.blue_gray, R.color.deep_purple, R.color.green}; 57 | 58 | private final int numberOfPages; 59 | 60 | public PagerAdapter(FragmentManager fm, int numberOfPages) { 61 | super(fm); 62 | this.numberOfPages = numberOfPages; 63 | } 64 | 65 | @Override 66 | public Fragment getItem(int position) { 67 | return SampleFragment.newInstance("fragment-#" + position, COLOR_ARRAY[position % COLOR_ARRAY.length]); 68 | } 69 | 70 | @Override 71 | public int getCount() { 72 | return numberOfPages; 73 | } 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/base/BasePresenterActivity.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v4.app.LoaderManager; 6 | import android.support.v4.content.Loader; 7 | import android.support.v7.app.AppCompatActivity; 8 | import android.util.Log; 9 | 10 | public abstract class BasePresenterActivity

, V> extends AppCompatActivity { 11 | 12 | private static final String TAG = "base-activity"; 13 | private static final int LOADER_ID = 101; 14 | private P presenter; 15 | 16 | @Override 17 | protected void onCreate(Bundle savedInstanceState) { 18 | super.onCreate(savedInstanceState); 19 | 20 | Loader

loader = getSupportLoaderManager().getLoader(loaderId()); 21 | if (loader == null) { 22 | initLoader(); 23 | } else { 24 | this.presenter = ((PresenterLoader

) loader).getPresenter(); 25 | onPresenterCreatedOrRestored(presenter); 26 | } 27 | } 28 | 29 | private void initLoader() { 30 | // LoaderCallbacks as an object, so no hint regarding Loader will be leak to the subclasses. 31 | getSupportLoaderManager().initLoader(loaderId(), null, new LoaderManager.LoaderCallbacks

() { 32 | @Override 33 | public final Loader

onCreateLoader(int id, Bundle args) { 34 | Log.i(TAG, "onCreateLoader"); 35 | return new PresenterLoader<>(BasePresenterActivity.this, getPresenterFactory(), tag()); 36 | } 37 | 38 | @Override 39 | public final void onLoadFinished(Loader

loader, P presenter) { 40 | Log.i(TAG, "onLoadFinished"); 41 | BasePresenterActivity.this.presenter = presenter; 42 | onPresenterCreatedOrRestored(presenter); 43 | } 44 | 45 | @Override 46 | public final void onLoaderReset(Loader

loader) { 47 | Log.i(TAG, "onLoaderReset"); 48 | BasePresenterActivity.this.presenter = null; 49 | } 50 | }); 51 | } 52 | 53 | @Override 54 | protected void onStart() { 55 | super.onStart(); 56 | Log.i(TAG, "onStart-" + tag()); 57 | presenter.onViewAttached(getPresenterView()); 58 | } 59 | 60 | @Override 61 | protected void onStop() { 62 | presenter.onViewDetached(); 63 | super.onStop(); 64 | Log.i(TAG, "onStop-" + tag()); 65 | } 66 | 67 | /** 68 | * String tag use for log purposes. 69 | */ 70 | @NonNull 71 | protected abstract String tag(); 72 | 73 | /** 74 | * Instance of {@link PresenterFactory} use to create a Presenter when needed. This instance should 75 | * not contain {@link android.app.Activity} context reference since it will be keep on rotations. 76 | */ 77 | @NonNull 78 | protected abstract PresenterFactory

getPresenterFactory(); 79 | 80 | /** 81 | * Hook for subclasses that deliver the {@link Presenter} before its View is attached. 82 | * Can be use to initialize the Presenter or simple hold a reference to it. 83 | */ 84 | protected abstract void onPresenterCreatedOrRestored(@NonNull P presenter); 85 | 86 | /** 87 | * Override in case of fragment not implementing Presenter interface 88 | */ 89 | @NonNull 90 | protected V getPresenterView() { 91 | return (V) this; 92 | } 93 | 94 | /** 95 | * Use this method in case you want to specify a spefic ID for the {@link PresenterLoader}. 96 | * By default its value would be {@link #LOADER_ID}. 97 | */ 98 | protected int loaderId() { 99 | return LOADER_ID; 100 | } 101 | } 102 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/base/BasePresenterFragment.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.base; 2 | 3 | import android.os.Bundle; 4 | import android.support.annotation.NonNull; 5 | import android.support.v4.app.Fragment; 6 | import android.support.v4.app.LoaderManager; 7 | import android.support.v4.content.Loader; 8 | import android.util.Log; 9 | 10 | public abstract class BasePresenterFragment

, V> extends Fragment { 11 | 12 | private static final String TAG = "base-fragment"; 13 | private static final int LOADER_ID = 101; 14 | 15 | private P presenter; 16 | 17 | @Override 18 | public void onActivityCreated(Bundle savedInstanceState) { 19 | super.onActivityCreated(savedInstanceState); 20 | Log.i(TAG, "onActivityCreated-" + tag()); 21 | 22 | Loader

loader = getLoaderManager().getLoader(loaderId()); 23 | if (loader == null) { 24 | initLoader(); 25 | } else { 26 | this.presenter = ((PresenterLoader

) loader).getPresenter(); 27 | onPresenterCreatedOrRestored(presenter); 28 | } 29 | } 30 | 31 | private void initLoader() { 32 | // LoaderCallbacks as an object, so no hint regarding loader will be leak to the subclasses. 33 | getLoaderManager().initLoader(loaderId(), null, new LoaderManager.LoaderCallbacks

() { 34 | @Override 35 | public final Loader

onCreateLoader(int id, Bundle args) { 36 | Log.i(TAG, "onCreateLoader-" + tag()); 37 | return new PresenterLoader<>(getContext(), getPresenterFactory(), tag()); 38 | } 39 | 40 | @Override 41 | public final void onLoadFinished(Loader

loader, P presenter) { 42 | Log.i(TAG, "onLoadFinished-" + tag()); 43 | BasePresenterFragment.this.presenter = presenter; 44 | onPresenterCreatedOrRestored(presenter); 45 | } 46 | 47 | @Override 48 | public final void onLoaderReset(Loader

loader) { 49 | Log.i(TAG, "onLoaderReset-" + tag()); 50 | BasePresenterFragment.this.presenter = null; 51 | } 52 | }); 53 | } 54 | 55 | @Override 56 | public void onStart() { 57 | super.onStart(); 58 | Log.i(TAG, "onStart-" + tag()); 59 | presenter.onViewAttached(getPresenterView()); 60 | } 61 | 62 | @Override 63 | public void onStop() { 64 | presenter.onViewDetached(); 65 | super.onStop(); 66 | Log.i(TAG, "onStop-" + tag()); 67 | } 68 | 69 | /** 70 | * String tag use for log purposes. 71 | */ 72 | @NonNull 73 | protected abstract String tag(); 74 | 75 | /** 76 | * Instance of {@link PresenterFactory} use to create a Presenter when needed. This instance should 77 | * not contain {@link android.app.Activity} context reference since it will be keep on rotations. 78 | */ 79 | @NonNull 80 | protected abstract PresenterFactory

getPresenterFactory(); 81 | 82 | /** 83 | * Hook for subclasses that deliver the {@link Presenter} before its View is attached. 84 | * Can be use to initialize the Presenter or simple hold a reference to it. 85 | */ 86 | protected abstract void onPresenterCreatedOrRestored(@NonNull P presenter); 87 | 88 | /** 89 | * Override in case of fragment not implementing Presenter interface 90 | */ 91 | @NonNull 92 | protected V getPresenterView() { 93 | return (V) this; 94 | } 95 | 96 | /** 97 | * Use this method in case you want to specify a spefic ID for the {@link PresenterLoader}. 98 | * By default its value would be {@link #LOADER_ID}. 99 | */ 100 | protected int loaderId() { 101 | return LOADER_ID; 102 | } 103 | } 104 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/base/Presenter.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.base; 2 | 3 | public interface Presenter { 4 | void onViewAttached(V view); 5 | 6 | void onViewDetached(); 7 | 8 | void onDestroyed(); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/base/PresenterFactory.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.base; 2 | 3 | /** 4 | * Creates a Presenter object. 5 | * @param presenter type 6 | */ 7 | public interface PresenterFactory { 8 | T create(); 9 | } 10 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/base/PresenterLoader.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.base; 2 | 3 | import android.content.Context; 4 | import android.support.v4.content.Loader; 5 | import android.util.Log; 6 | 7 | final class PresenterLoader extends Loader { 8 | 9 | private final PresenterFactory factory; 10 | private final String tag; 11 | private T presenter; 12 | 13 | PresenterLoader(Context context, PresenterFactory factory, String tag) { 14 | super(context); 15 | this.factory = factory; 16 | this.tag = tag; 17 | } 18 | 19 | @Override 20 | protected void onStartLoading() { 21 | Log.i("loader", "onStartLoading-" + tag); 22 | 23 | // if we already own a presenter instance, simply deliver it. 24 | if (presenter != null) { 25 | deliverResult(presenter); 26 | return; 27 | } 28 | 29 | // Otherwise, force a load 30 | forceLoad(); 31 | } 32 | 33 | @Override 34 | protected void onForceLoad() { 35 | Log.i("loader", "onForceLoad-" + tag); 36 | 37 | // Create the Presenter using the Factory 38 | presenter = factory.create(); 39 | 40 | // Deliver the result 41 | deliverResult(presenter); 42 | } 43 | 44 | @Override 45 | public void deliverResult(T data) { 46 | super.deliverResult(data); 47 | Log.i("loader", "deliverResult-" + tag); 48 | } 49 | 50 | @Override 51 | protected void onStopLoading() { 52 | Log.i("loader", "onStopLoading-" + tag); 53 | } 54 | 55 | @Override 56 | protected void onReset() { 57 | Log.i("loader", "onReset-" + tag); 58 | if (presenter != null) { 59 | presenter.onDestroyed(); 60 | presenter = null; 61 | } 62 | } 63 | 64 | public T getPresenter() { 65 | return presenter; 66 | } 67 | } 68 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/presenter/SamplePresenter.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.presenter; 2 | 3 | import de.czyrux.mvploadersample.base.Presenter; 4 | 5 | public class SamplePresenter implements Presenter { 6 | 7 | private final String title; 8 | private SampleView view; 9 | private int count = 0; 10 | 11 | public SamplePresenter(String title) { 12 | this.title = title; 13 | } 14 | 15 | @Override 16 | public void onViewAttached(SampleView view) { 17 | this.view = view; 18 | this.count++; 19 | this.view.showMessage(title + ". View attached " + count + " times"); 20 | } 21 | 22 | @Override 23 | public void onViewDetached() { 24 | this.view = null; 25 | } 26 | 27 | @Override 28 | public void onDestroyed() { 29 | // Nothing to clean up 30 | } 31 | } 32 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/presenter/SamplePresenterFactory.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.presenter; 2 | 3 | import de.czyrux.mvploadersample.base.PresenterFactory; 4 | 5 | public class SamplePresenterFactory implements PresenterFactory{ 6 | 7 | private final String title; 8 | 9 | public SamplePresenterFactory(String title) { 10 | this.title = title; 11 | } 12 | 13 | @Override 14 | public SamplePresenter create() { 15 | return new SamplePresenter(title); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /app/src/main/java/de/czyrux/mvploadersample/presenter/SampleView.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample.presenter; 2 | 3 | public interface SampleView { 4 | void showMessage(String text); 5 | } 6 | -------------------------------------------------------------------------------- /app/src/main/res/layout/activity_mvp.xml: -------------------------------------------------------------------------------- 1 | 2 | 7 | 8 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_pager.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 11 | 12 | 13 | -------------------------------------------------------------------------------- /app/src/main/res/layout/fragment_single.xml: -------------------------------------------------------------------------------- 1 | 6 | 7 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /app/src/main/res/menu/activity_menu.xml: -------------------------------------------------------------------------------- 1 | 2 |

3 | 4 | 7 | 8 | 11 | 12 | 15 | -------------------------------------------------------------------------------- /app/src/main/res/mipmap-hdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/czyrux/MvpLoaderSample/9ab7a92e09df7772d49da90747c50890886e96bd/app/src/main/res/mipmap-hdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-mdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/czyrux/MvpLoaderSample/9ab7a92e09df7772d49da90747c50890886e96bd/app/src/main/res/mipmap-mdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/czyrux/MvpLoaderSample/9ab7a92e09df7772d49da90747c50890886e96bd/app/src/main/res/mipmap-xhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/czyrux/MvpLoaderSample/9ab7a92e09df7772d49da90747c50890886e96bd/app/src/main/res/mipmap-xxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/mipmap-xxxhdpi/ic_launcher.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/czyrux/MvpLoaderSample/9ab7a92e09df7772d49da90747c50890886e96bd/app/src/main/res/mipmap-xxxhdpi/ic_launcher.png -------------------------------------------------------------------------------- /app/src/main/res/values-w820dp/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 5 | 64dp 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #3F51B5 4 | #303F9F 5 | #FF4081 6 | 7 | #bbdefb 8 | #dce775 9 | #fff176 10 | #8D6E63 11 | #FFCCBC 12 | #78909C 13 | #B39DDB 14 | 15 | 16 | -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 16dp 4 | 16dp 5 | 6 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | MvpLoaderSample 3 | 4 | 5 | Hello blank fragment 6 | 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 12 | -------------------------------------------------------------------------------- /app/src/test/java/de/czyrux/mvploadersample/ExampleUnitTest.java: -------------------------------------------------------------------------------- 1 | package de.czyrux.mvploadersample; 2 | 3 | import org.junit.Test; 4 | 5 | import static org.junit.Assert.*; 6 | 7 | /** 8 | * To work on unit tests, switch the Test Artifact in the Build Variants view. 9 | */ 10 | public class ExampleUnitTest { 11 | @Test 12 | public void addition_isCorrect() throws Exception { 13 | assertEquals(4, 2 + 2); 14 | } 15 | } -------------------------------------------------------------------------------- /build.gradle: -------------------------------------------------------------------------------- 1 | // Top-level build file where you can add configuration options common to all sub-projects/modules. 2 | 3 | buildscript { 4 | repositories { 5 | jcenter() 6 | } 7 | dependencies { 8 | classpath 'com.android.tools.build:gradle:2.1.2' 9 | 10 | // NOTE: Do not place your application dependencies here; they belong 11 | // in the individual module build.gradle files 12 | } 13 | } 14 | 15 | allprojects { 16 | repositories { 17 | jcenter() 18 | } 19 | } 20 | 21 | task clean(type: Delete) { 22 | delete rootProject.buildDir 23 | } 24 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | 3 | # IDE (e.g. Android Studio) users: 4 | # Gradle settings configured through the IDE *will override* 5 | # any settings specified in this file. 6 | 7 | # For more details on how to configure your build environment visit 8 | # http://www.gradle.org/docs/current/userguide/build_environment.html 9 | 10 | # Specifies the JVM arguments used for the daemon process. 11 | # The setting is particularly useful for tweaking memory settings. 12 | # Default value: -Xmx10248m -XX:MaxPermSize=256m 13 | # org.gradle.jvmargs=-Xmx2048m -XX:MaxPermSize=512m -XX:+HeapDumpOnOutOfMemoryError -Dfile.encoding=UTF-8 14 | 15 | # When configured, Gradle will run in incubating parallel mode. 16 | # This option should only be used with decoupled projects. More details, visit 17 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 18 | # org.gradle.parallel=true -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env bash 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 10 | DEFAULT_JVM_OPTS="" 11 | 12 | APP_NAME="Gradle" 13 | APP_BASE_NAME=`basename "$0"` 14 | 15 | # Use the maximum available, or set MAX_FD != -1 to use that value. 16 | MAX_FD="maximum" 17 | 18 | warn ( ) { 19 | echo "$*" 20 | } 21 | 22 | die ( ) { 23 | echo 24 | echo "$*" 25 | echo 26 | exit 1 27 | } 28 | 29 | # OS specific support (must be 'true' or 'false'). 30 | cygwin=false 31 | msys=false 32 | darwin=false 33 | case "`uname`" in 34 | CYGWIN* ) 35 | cygwin=true 36 | ;; 37 | Darwin* ) 38 | darwin=true 39 | ;; 40 | MINGW* ) 41 | msys=true 42 | ;; 43 | esac 44 | 45 | # Attempt to set APP_HOME 46 | # Resolve links: $0 may be a link 47 | PRG="$0" 48 | # Need this for relative symlinks. 49 | while [ -h "$PRG" ] ; do 50 | ls=`ls -ld "$PRG"` 51 | link=`expr "$ls" : '.*-> \(.*\)$'` 52 | if expr "$link" : '/.*' > /dev/null; then 53 | PRG="$link" 54 | else 55 | PRG=`dirname "$PRG"`"/$link" 56 | fi 57 | done 58 | SAVED="`pwd`" 59 | cd "`dirname \"$PRG\"`/" >/dev/null 60 | APP_HOME="`pwd -P`" 61 | cd "$SAVED" >/dev/null 62 | 63 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 64 | 65 | # Determine the Java command to use to start the JVM. 66 | if [ -n "$JAVA_HOME" ] ; then 67 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 68 | # IBM's JDK on AIX uses strange locations for the executables 69 | JAVACMD="$JAVA_HOME/jre/sh/java" 70 | else 71 | JAVACMD="$JAVA_HOME/bin/java" 72 | fi 73 | if [ ! -x "$JAVACMD" ] ; then 74 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 75 | 76 | Please set the JAVA_HOME variable in your environment to match the 77 | location of your Java installation." 78 | fi 79 | else 80 | JAVACMD="java" 81 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 82 | 83 | Please set the JAVA_HOME variable in your environment to match the 84 | location of your Java installation." 85 | fi 86 | 87 | # Increase the maximum file descriptors if we can. 88 | if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then 89 | MAX_FD_LIMIT=`ulimit -H -n` 90 | if [ $? -eq 0 ] ; then 91 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 92 | MAX_FD="$MAX_FD_LIMIT" 93 | fi 94 | ulimit -n $MAX_FD 95 | if [ $? -ne 0 ] ; then 96 | warn "Could not set maximum file descriptor limit: $MAX_FD" 97 | fi 98 | else 99 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 100 | fi 101 | fi 102 | 103 | # For Darwin, add options to specify how the application appears in the dock 104 | if $darwin; then 105 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 106 | fi 107 | 108 | # For Cygwin, switch paths to Windows format before running java 109 | if $cygwin ; then 110 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 111 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 112 | JAVACMD=`cygpath --unix "$JAVACMD"` 113 | 114 | # We build the pattern for arguments to be converted via cygpath 115 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 116 | SEP="" 117 | for dir in $ROOTDIRSRAW ; do 118 | ROOTDIRS="$ROOTDIRS$SEP$dir" 119 | SEP="|" 120 | done 121 | OURCYGPATTERN="(^($ROOTDIRS))" 122 | # Add a user-defined pattern to the cygpath arguments 123 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 124 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 125 | fi 126 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 127 | i=0 128 | for arg in "$@" ; do 129 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 130 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 131 | 132 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 133 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 134 | else 135 | eval `echo args$i`="\"$arg\"" 136 | fi 137 | i=$((i+1)) 138 | done 139 | case $i in 140 | (0) set -- ;; 141 | (1) set -- "$args0" ;; 142 | (2) set -- "$args0" "$args1" ;; 143 | (3) set -- "$args0" "$args1" "$args2" ;; 144 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 145 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 146 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 147 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 148 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 149 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 150 | esac 151 | fi 152 | 153 | # Split up the JVM_OPTS And GRADLE_OPTS values into an array, following the shell quoting and substitution rules 154 | function splitJvmOpts() { 155 | JVM_OPTS=("$@") 156 | } 157 | eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS 158 | JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME" 159 | 160 | exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@" 161 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 12 | set DEFAULT_JVM_OPTS= 13 | 14 | set DIRNAME=%~dp0 15 | if "%DIRNAME%" == "" set DIRNAME=. 16 | set APP_BASE_NAME=%~n0 17 | set APP_HOME=%DIRNAME% 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windowz variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | if "%@eval[2+2]" == "4" goto 4NT_args 53 | 54 | :win9xME_args 55 | @rem Slurp the command line arguments. 56 | set CMD_LINE_ARGS= 57 | set _SKIP=2 58 | 59 | :win9xME_args_slurp 60 | if "x%~1" == "x" goto execute 61 | 62 | set CMD_LINE_ARGS=%* 63 | goto execute 64 | 65 | :4NT_args 66 | @rem Get arguments from the 4NT Shell from JP Software 67 | set CMD_LINE_ARGS=%$ 68 | 69 | :execute 70 | @rem Setup the command line 71 | 72 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 73 | 74 | @rem Execute Gradle 75 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 76 | 77 | :end 78 | @rem End local scope for the variables with windows NT shell 79 | if "%ERRORLEVEL%"=="0" goto mainEnd 80 | 81 | :fail 82 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 83 | rem the _cmd.exe /c_ return code! 84 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 85 | exit /b 1 86 | 87 | :mainEnd 88 | if "%OS%"=="Windows_NT" endlocal 89 | 90 | :omega 91 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | --------------------------------------------------------------------------------