Skip to content

Instantly share code, notes, and snippets.

@patrickhammond
Last active August 29, 2015 14:22
Show Gist options
  • Save patrickhammond/9b54a5e82e07c5a265c0 to your computer and use it in GitHub Desktop.
Save patrickhammond/9b54a5e82e07c5a265c0 to your computer and use it in GitHub Desktop.
Work in progress...
public class MyActivity extends Activity {
private static final String STATEFUL_COUNT = “count”;
private int count;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
count = savedInstanceState.getInt(STATEFUL_COUNT);
} else {
count = 1;
}
... normal activity stuff ...
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.put(STATEFUL_COUNT, count);
}
}
public class MyActivity extends Activity {
@Stateful(initial=1)
private int count;
@Override
public void onCreate(Bundle savedInstance) {
super.onCreate(savedInstanceState);
... normal activity stuff (state has already been restored) ...
}
}
It is save to ignore this file if you are just reading sample code!!!
onCreate(savedInstanceState) {
super.onCreate(savedInstanceState)
(( "injected" state magic ))
... useful implementation
}
onSaveInstanceState(out) {
super.onSaveInstanceState(out)
(( "injected" state magic ))
... optional interesting implementation (otherwise explicit onSaveInstanceState is not required)
}
It is save to ignore this file if you are just reading sample code!!!
(( "injected" state magic ))
onCreate(savedInstanceState) {
super.onCreate(savedInstanceState)
... useful implementation
}
onSaveInstanceState(out) {
super.onSaveInstanceState(out)
... optional interesting implementation (otherwise explicit onSaveInstanceState is not required)
}
(( "injected" state magic )) <-- Not perfect due to potential of state clobbering.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment