Skip to content
This repository was archived by the owner on Feb 26, 2023. It is now read-only.

77 save instance state#101

Merged
pyricau merged 14 commits intodevelopfrom
77_Save_Instance_State
Feb 17, 2012
Merged

77 save instance state#101
pyricau merged 14 commits intodevelopfrom
77_Save_Instance_State

Conversation

@mathieuboniface
Copy link
Copy Markdown
Contributor

This is a first implementation of @SaveOnActivityDestroy annotation explained on issue #77.

@pyricau
Copy link
Copy Markdown
Contributor

pyricau commented Feb 16, 2012

Looks great !!

A few notes :

  • @SaveOnActivityDestroy is a bit long and not totally true : it only saves the state is the activity is meant to be rebuilt later on. For instance, if you call finish() on an activity, onSaveInstanceState() won't be called.
  • What do you think about using @InstanceState instead ?
  • I'm currently reviewing the generated code. The part related to wrapper classes is not symmetric :
    @SaveOnActivityDestroy
    Boolean myBooleanObject;

// ...

        if (myBooleanObject!= null) {
            bundle.putBoolean("myBooleanObject", myBooleanObject);
        }

// ...

           myBooleanObject = savedInstanceState.getBoolean("myBooleanObject");

android.os.Bundle.getBoolean(String) :

Returns the value associated with the given key, or false if no mapping of the desired type exists for the given key.

So basically, if myBooleanObject is null when saved, then it will be recreated as false.

@pyricau
Copy link
Copy Markdown
Contributor

pyricau commented Feb 16, 2012

The loading code could in fact be :

        if (savedInstanceState.containsKey("myBooleanObject")) {
            myBooleanObject = savedInstanceState.getBoolean("myBooleanObject");
        }

Another option, maybe even more appropriate, would be to use Serializable for wrapper types :

    @SaveOnActivityDestroy
    Boolean myBooleanObject;

// ...

            bundle.putSerializable("myBooleanObject", myBooleanObject);

// ...

           myBooleanObject = (Boolean) savedInstanceState.getSerializable("myBooleanObject");

I think the latter is safer, because it handle the case where you have an init value :

    @SaveOnActivityDestroy
    Boolean myBooleanObject = true;

If at some point you do myBooleanObject = null and then the activity state is saved :

  • If we skip null primitive types, it will be recreated to true
  • If we use the standard Serializable mechanism, it will be recreated to null

@pyricau
Copy link
Copy Markdown
Contributor

pyricau commented Feb 17, 2012

A few more notes :

Generics

Generics don't work yet. Lists work because they are part of the "trivial list", but look at this :

    @SaveOnActivityDestroy
    MyGenericSerializableBean<MySerializableBean> myGenericSerializableBean;

It generates the following compile error : Unrecognized type. Please let your attribute be primitive or implement Serializable or Parcelable.

API Level 8

In the processor, the following code was commented :

        // Added on API level 8
        // methodSuffixNameByTypeName.put("java.lang.CharSequence[]", "CharSequenceArray");
        // methodSuffixNameByTypeName.put("java.util.ArrayList<java.lang.CharSequence>", "CharSequenceArrayList");

I removed it, we shouldn't leave that kind of commented dead code somewhere. However, that doesn't solve the issue you were talking about.

I think we can decide to not support such types, and resort to saving them as Serializable instead.

@pyricau
Copy link
Copy Markdown
Contributor

pyricau commented Feb 17, 2012

@matboniface What's your opinion regarding generics ?

pyricau pushed a commit that referenced this pull request Feb 17, 2012
@pyricau pyricau merged commit 3c77da2 into develop Feb 17, 2012
krischik pushed a commit to krischik/androidannotations that referenced this pull request Apr 13, 2013
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants