Skip to content

Instantly share code, notes, and snippets.

@digitalbuddha
Last active February 7, 2018 07:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save digitalbuddha/5bbf2feccf8ab5ecc4ed to your computer and use it in GitHub Desktop.
Save digitalbuddha/5bbf2feccf8ab5ecc4ed to your computer and use it in GitHub Desktop.
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