3 layouts and an activity

0

I have an actyvity_main, containing 3 radiobuttons (each one corresponds to a layout), and a button. After the user selects the option, he clicks the button, which makes a call to another activity, where in it, I need to do with the selected layout.

public void onClick(View view) {
        int checkeRadioButtonId = escolhaRadio.getCheckedRadioButtonId();
        switch (checkeRadioButtonId) {
            case R.id.rdb1:
                Intent opcao8 = new Intent(this, Activity_Jogo.class);
                setContentView(R.layout.layout_8);
                startActivity(opcao8);
                break;
            case R.id.rdb2:
                Intent opcao10 = new Intent(this, Activity_Jogo.class);
                setContentView(R.layout.layout_10);
                startActivity(opcao10);
                break;
            case R.id.rdb3:
                Intent opcao12 = new Intent(this, Activity_Jogo.class);
                setContentView(R.layout.layout_12);
                startActivity(opcao12);
        }
    }

In this way I'm doing it, it calls the layout and, after appearing it instantly calls the activity, leaving only the activity with its default layout.

    
asked by anonymous 24.05.2015 / 16:00

1 answer

1

The simplest way would be to pass the resource from layout to Extras , and recover at next Activity :

public void onClick(View view) {
    int checkeRadioButtonId = escolhaRadio.getCheckedRadioButtonId();
    Intent i = new Intent(this, Activity_Jogo.class);

    switch (checkeRadioButtonId) {
        case R.id.rdb1:
            opcao10.putExtra("opcao", R.layout.layout_8);
            break;
        case R.id.rdb2:
            opcao10.putExtra("opcao", R.layout.layout_10);
            break;
        case R.id.rdb3:
            opcao10.putExtra("opcao", R.layout.layout_12);
            break;
    }

    startActivity(i);
}
In the onCreate method of your Activity_Jogo you would retrieve this value and use it as contentView :

@Override
protected onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    int layout = getIntent().getIntExtra("opcao", 0);

    // Se ninguem passou um layout como parametro,
    // voce pode usar um padrao ou fechar a Activity
    if(layout == 0) {
        finish();
    }

    setContentView(layout);

    // Restante do seu processamento...
}
    
24.05.2015 / 17:02