Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
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

Copy link
Copy link
@mathieuboniface

Description

@mathieuboniface
Issue body actions

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

}
Reactions are currently unavailable

Metadata

Metadata

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

    Morty Proxy This is a proxified and sanitized view of the page, visit original site.