material-remixer-android
material-remixer-android copied to clipboard
Consider scheduling VariableMethods on the next event loop from RemixerBinder.bind(this)
I have code like this:
private View target;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
RemixerBinder.bind(this); // [1]
RemixerFragment remixerFragment = RemixerFragment.newInstance();
remixerFragment.attachToButton(this, (Button) findViewById(R.id.remixer_button));
target = findViewById(R.id.target); // [3]
}
@BooleanVariableMethod(defaultValue = true)
public void setFoo(Boolean foo) {
target.bar(); // [2]
}
This throws a NullPointerException at [2]
.
In this simple example, [1]
is called first, which synchronously calls [2]
. This is a problem because [3]
has not yet run.
One solution is to teach clients to perform all dependent initializations before RemixerBinder.bind(this)
. This has the problem of education and that it's not always obvious what is a dependent initialization and what can happen later.
An alternative solution is for RemixerBinder.bind(this)
to schedule (Activity#runOnUiThread
, Handler#post
) the VariableMethods to run on the next event loop iteration, giving onCreate()
a chance to finish.