excilys/androidannotations — Issue #77: Add support to save current activity instance state before destruction. 
Author: matboniface
Created: 2012-01-21 18:04:10+00:00
State: open

Save instance state between activity destruction/creation is really paintful and generate a lot of boilerplate code. As we do not like this approach because ((lot of code) == (lot of bug)) (and also to save my carpal tunnel), i think we could provide a solution.

Here is a classic way to handle activity instance destruction/creation :

```java
public class SaveInstanceStateActivity extends Activity {

private int myInt;

private MyBean myBean;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout.main);

if (savedInstanceState != null) {
myInt = savedInstanceState.getInt("myInt");
myBean = (MyBean) savedInstanceState.getSerializable("myBean");
}
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("myInt", myInt);
outState.putSerializable("myBean", myBean);
}

}
```

By introducing the `@Save` annotation the written code could be like this :

```java
@EActivity(R.layout.main)
public class SaveInstanceStateActivity extends Activity {

@SaveOnActivityDestroy
int myInt;

@SaveOnActivityDestroy
MyBean myBean;

}
```

And the generated code like this :

```java
public class SaveInstanceStateActivity_ extends SaveInstanceStateActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

if (savedInstanceState != null) {
myInt = savedInstanceState.getInt("myInt");
myBean = (MyBean) savedInstanceState.getSerializable("myBean");
}

}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("myInt", myInt);
outState.putSerializable("myBean", myBean);
}

}
```