Skip to content

NonSavable annotation

MarcinMoskala edited this page Feb 15, 2017 · 1 revision

NonSavable is annotation used before Activities when we don't want save parts to be generated. This parts are shown on [Activity equivalent example](Activity equivalent example), but sometimes (often for code optimization) we don't want to save state of variables and in case of activity restore, just use values provided in the argument.

Here is usage example:

@NonSavable
public class MainActivity extends BaseActivity {

    @Arg String name;
    @Arg int id;
    @Arg char grade;
    @Arg boolean passing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ActivityStarter.fill(this, savedInstanceState);
    }

Will work like:

public class MainActivity extends BaseActivity {

    private static String NAME_KEY = "nameArg";
    private static String ID_KEY = "idArg";
    private static String GRADE_KEY = "gradeArg";
    private static String PASSING_KEY = "passingArg";

    public static void start(Context context, String name, int id, char grade, boolean passing) {
        Intent intent = new Intent(context, MainActivity.class);
        intent.putExtra(NAME_KEY, name);
        intent.putExtra(ID_KEY, id);
        intent.putExtra(GRADE_KEY, grade);
        intent.putExtra(PASSING_KEY, passing);
        context.startActivity(intent);
    }

    String name;
    int id;
    char grade;
    boolean passing;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Intent intent = getIntent();
        name = intent.getStringExtra(NAME_KEY);
        id = intent.getIntExtra(ID_KEY, -1);
        grade = intent.getCharExtra(GRADE_KEY, 'a');
        passing = intent.getBooleanExtra(PASSING_KEY, false);
    }
}

More examples of Activities and what will be generated from them can be found here.

Clone this wiki locally