GNStateManager - noxiouswinter/gnlib_android GitHub Wiki

GNStateManager makes saving and retrieving the state/fields of an Activity etc as easy as annotating the required fields and adding a call to the GNStateManager in the OnCreate and OnPause methods. Custom objects are supported and their fields can be selectively saved/retrieved too.

Usage

In your Activity, annotate the fields you need to persist with the '@GNState' annotation.

public class TestActivity extends Activity {

    @GNState
    private StudentInfo studentInfo;

Also annotate the class definition and the required fields of any custom object.

@GNState
public class StudentInfo {

    @GNState
    public String name;

    @GNState
    public int age = 0;
}

In the onCreate method of the Activity, initialize GNStateManagerFactory(you need to do this only once anywhere) and pass in the Activity object to the retrieve function.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //Required only once.
    GNStateManagerFactory.init(this);
    //Populate this object with the state field objects.
    GNStateManagerFactory.get().retrieve(this);
}

Also in the onPause function, call the store function with the Activity object.

@Override
protected void onPause() {
    GNStateManagerFactory.get().store(this);
    super.onPause();
}

Limitations

GNStateManager will not work well with Activities or other classes with multiple instances. The state objects like StudentInfo in above example but does not need to be singletons.