yatatsu / AutoBundle

AutoBundle generates boilerplate code for field binding with android.os.Bundle
Apache License 2.0
135 stars 13 forks source link

IllegalStateException #18

Closed ssyijiu closed 7 years ago

ssyijiu commented 7 years ago

I use the follow code in Activity#onCreate

        if (savedInstanceState != null) {
             AutoBundle.bind(this, savedInstanceState);
        } else if(getIntent() != null){
            AutoBundle.bind(this, getIntent());
        }

and @AutoBundleField String status,name,password; the name and password I get from a Intent, the status I only want to Save The State, when I first open the Acticity from other activity,AutoBundle.bind(this, getIntent()) execute , and I don't put the status in intent, finally there is a IllegalStateException .

I find the cause:

if (source.containsKey("status")) {
      target.status = (String) source.getString("status");
    } else {
      throw new IllegalStateException("status is required, but not found in the bundle.");
    }
yatatsu commented 7 years ago

How about to use requiredoption? @AutoBundleField required to be contain in Bundle as default. So set false to annotation.

Here is sample code...

public class LoginActivity extends AppCompatActivity {
  @AutoBundleField(required = false) String status = "Default";
  @AutoBundleField String name, password;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (savedInstanceState == null) {
      AutoBundle.bind(this, getIntent());// status == "Default"
    } else {
      AutoBundle.bind(this, savedInstanceState);// status restored from state.
    }
  }

  @Override protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    AutoBundle.pack(this, outState);
  }
}
ssyijiu commented 7 years ago

Thanks for your answer . It solved my problem perfectly.