[Library] Akatsuki- an Android library that handles state restoration via annotations

Search This thread

Tom Lin

New member
Apr 10, 2010
3
0
Taichung
Akatsuki
You can skip the intro and just look at the examples below and check out the GitHub page(down below) if you already know what I'm talking about through the title.​

Motivation/Intro
Coming from standard desktop app development, configuration changes such as screen rotation is new. The way Android handles these configuration changes is by restarting your app(the current Activity, to be precise). You lose all your fields so you have to retain them some how, here's a typical case:

PHP:
public class MainActivity extends Activity {

    private static final String MY_STRING = "myString";
    private static final String MY_INT = "myInt";
    private static final String ACCOUNT = "account";

    String myString;
    int myInt;
    Account account;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myString = savedInstanceState.getString(MY_STRING);
        myInt = savedInstanceState.getInt(MY_INT);
        account = savedInstanceState.getParcelable(ACCOUNT);
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(MY_STRING, myString);
        outState.putInt(MY_INT, myInt);
        outState.putParcelable(ACCOUNT, account);
    }
}

As you can see, you need extra constants and lots of boilerplates just to retain the values of some fields. Java is already kinda verbose, Android is making things worse by requiring you retain the fields manually.
Akatsuki eliminates all those boilerplate, here's what the example from above looks like with Akatsuki:

PHP:
public class MainActivity extends Activity {

    @Retained String myString;
    @Retained int myInt;
    @Retained Account account; // Account implements Parcelable

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Akatsuki.restore(this, savedInstanceState);
        //everything restored!   
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Akatsuki.save(this, outState);
    }
}
And that's it, the fields are retained.

Performance Impact
Next to 0, the whole processing is done at compile time.

Magic?
No black magic used, all this is possible because of JSR269 (Annotation Processing Tool). There are similar libraries out there such as Butterknife

Notable Features
  • Supports most commonly used types (beyond what Bundle supports!)
  • Pluggable type conveter for your custom types
  • Near zero runtime performance impact
  • Integration tests included
  • Generic type support
  • View state support
  • Can be used as an Object to Bundle serialization solution as well

License
Apache 2.0

GitHub page with download links (gradle dependencies) and a more detailed README:

https://github.com/tom91136/Akatsuki

(Sorry, can't have links because I got less than 10 posts for now)
 
Last edited: