Last active
February 7, 2018 07:58
-
-
Save digitalbuddha/5bbf2feccf8ab5ecc4ed to your computer and use it in GitHub Desktop.
This file contains 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
public class BaseActivity | |
{ | |
protected void onCreate(@Nullable Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
bundleService = new BundleService(savedInstanceState, getIntent().getExtras()); | |
} | |
.... | |
protected void onSaveInstanceState(Bundle outState) { | |
outState.putAll(bundleService.getAll()); | |
super.onSaveInstanceState(outState); | |
} | |
public BundleService getBundleService() { | |
return bundleService; | |
} | |
} | |
public class BundleService { | |
private Bundle data; | |
public BundleService(Bundle savedState, Bundle intentExtras) { | |
data = new Bundle(); | |
if (savedState != null) { | |
data.putAll(savedState); | |
} | |
if (intentExtras != null) { | |
data.putAll(intentExtras); | |
} | |
} | |
public Object get(String key) { | |
return data.get(key); | |
} | |
public boolean contains(String key) { | |
return data.containsKey(key); | |
} | |
public Bundle getAll() { | |
return data; | |
} | |
} | |
@Module | |
public class BundleModule { | |
@Provides | |
@ScopeActivity | |
public Bundle provideBundle(Activity context) { | |
return context.getIntent().getExtras() == null ? new Bundle() : context.getIntent().getExtras(); | |
} | |
@Provides | |
@ScopeActivity | |
public Intent provideIntent(Activity context) { | |
return context.getIntent() == null ? new Intent() : context.getIntent(); | |
} | |
@Provides | |
@ScopeActivity | |
public BundleService provideBundleService(Activity context) { | |
return ((BaseAppCompatActivity) context).getBundleService(); | |
} | |
@Provides | |
@ScopeActivity | |
public Optional<Asset> provideAsset(BundleService bundleService) { | |
Asset asset = (Asset) bundleService.get(MediaActivity.EXTRA_ASSET); | |
return Optional.fromNullable(asset); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment