Skip to content

Activity equivalent example

Marcin Moskała edited this page Apr 2, 2017 · 5 revisions

This, simple Activity:

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

}

supposing, that we inclide in BaseActivity: (one for all classes)

public class BaseActivity extends AppCompatActivity {

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

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        ActivityStarter.save(this, outState);
    }
}

is in fact equivalent of:

public class MainActivity extends BaseActivity {

    private static String NAME_KEY = "com.example.activitystarter.nameStarterKey";
    private static String ID_KEY = "com.example.activitystarter.idStarterKey";
    private static String GRADE_KEY = "com.example.activitystarter.gradeStarterKey";
    private static String PASSING_KEY = "com.example.activitystarter.passingStarterKey";

    public static Intent getIntent(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);
        return intent;
    }

    public static void start(Context context, String name, int id, char grade, boolean passing) {
        Intent intent = getIntent(context, name, id, grade, passing);
        context.startActivity(intent);
    }

    public static void startWithFlags(Context context, String name, int id, char grade, boolean passing, int flags) {
        Intent intent = getIntent(context, name, id, grade, passing);
        intent.addFlags(flags);
        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);

        if (savedInstanceState != null && savedInstanceState.containsKey(NAME_KEY)) {
            activity.name = savedInstanceState.getString(NAME_KEY);
        } else {
            if (intent.hasExtra(NAME_KEY))
                activity.name = intent.getStringExtra(NAME_KEY);
        }
        if (savedInstanceState != null && savedInstanceState.containsKey(ID_KEY)) {
            activity.id = savedInstanceState.getInt(ID_KEY, 0);
        } else {
            if (intent.hasExtra(ID_KEY))
                activity.id = intent.getIntExtra(ID_KEY, 0);
        }
        if (savedInstanceState != null && savedInstanceState.containsKey(GRADE_KEY)) {
            activity.grade = savedInstanceState.getChar(GRADE_KEY, '\u0000');
        } else {
            if (intent.hasExtra(GRADE_KEY))
                activity.grade = intent.getCharExtra(GRADE_KEY, '\u0000');
        }
        if (savedInstanceState != null && savedInstanceState.containsKey(PASSING_KEY)) {
            activity.passing = savedInstanceState.getBoolean(PASSING_KEY, false);
        } else {
            if (intent.hasExtra(PASSING_KEY))
                activity.passing = intent.getBooleanExtra(PASSING_KEY, false);
        }
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putString(NAME_KEY, name);
        outState.putInt(ID_KEY, id);
        outState.putChar(GRADE_KEY, grade);
        outState.putBoolean(PASSING_KEY, passing);
    }
}
Clone this wiki locally