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

Add support to save current activity instance state before destruction.  #77

Description

@mathieuboniface

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 :

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 @SaveOnActivityDestroy annotation the written code could be like this :

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

    @SaveOnActivityDestroy
    int myInt;

    @SaveOnActivityDestroy
    MyBean myBean;  

}

And the generated code like this :

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);
    }

}

Activity

  1. adevelop commented on Jan 22, 2012

    @adevelop

    When will this feature be available for download?

  2. pyricau commented on Jan 23, 2012

    @pyricau
    Contributor

    When someone will have implemented it, and then on the next release :) . This is an interesting and new idea, we need to discuss all the details :) .

  3. pyricau commented on Feb 17, 2012

    @pyricau
    Contributor

    We finally decided to use the following annotation :

    @EActivity(R.layout.main)
    public class SaveInstanceStateActivity extends Activity {
    
        @InstanceState
        int myInt;
    
        @InstanceState
        MyBean myBean;  
    
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions