morristech
2/7/2018 - 7:58 AM

bundleService.java

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);
    }
}