Balkyto
Balkyto

Reputation: 1520

Can One Android class handle multiple activities and how to start correct one?

I have specific need for "settings" setup, where I have list with 4 options (Age, Height, Weight and Gender). Now, each list item MUST open new "view" (activity, window), and it actually does not do much. EG, Gender is radio buttons and "OK" button, and then saves Gender in shared prefs.

So, I had great Idea to create "Settings" class, that then somehow handles this. When item 1 is selecte, it would display layout1.xml and call function1 from Settings class.

Item2 calls layout2.xml and calls function2 from Settings class.

Is this good approach, or should I create class for every frame, and have small "GenderSelector" class that actually does nothing but handles radio button?

Upvotes: 0

Views: 374

Answers (1)

Kirill Gamazkov
Kirill Gamazkov

Reputation: 3397

  1. You can use dialogs.
  2. Intent you are using to start the activity can hold primitive data that is accessible for the activity being started. Then you can conditionally populate started activity with controls. And after all, you can return result to the first activity.

Code snippet to start the activity with specified mode (mode constants are defined below):

public class SettingsActivity extends Activity
{
    public static final String EXTRA_MODE = "your_mode_extra_name";
    public static final int MODE_AGE = 0;
    public static final int MODE_GENDER = 1;

    private static final int MODE_DEFAULT = MODE_AGE;

    @Override
    public void onCreate(Bundle savedInstance)
    {
        int mode = getIntent().getIntExtra(EXTRA_MODE, MODE_DEFAULT);

        switch(mode)
        {
            case MODE_AGE:
                setContentView(R.layout.age_mode_layout);
                //TODO: find controls by ID and set their event handlers here
                break;

             case MODE_GENDER:
                setContentView(R.layout.age_mode_gender);
                //TODO: find controls by ID and set their event handlers here
                break;
        }
    }
}

Code snippet to return the gender:

public class SettingsActivity extends Activity
{
    public static final String EXTRA_GENDER = "your_gender_extra_name";

    public static final int GENDER_FEMALE = 0;
    public static final int GENDER_MALE = 1;

    private void returnGender(int gender)
    {
        setResult(RESULT_OK, new Intent().putExtra(EXTRA_GENDER, gender));
        finish();
    }
}

And finally showing the settings activity and getting back the result:

public class MainActivity extends Activity
{
    //actually we could use mode constants from SettingsActivity;
    //just to show that generally this is separate constants set
    private static final int ID_AGE = 0;
    private static final int ID_GENDER = 1;

    private void requestAge()
    {
        Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
        intent.putExtra(SettingsActivity.EXTRA_MODE, SettingsActivity.MODE_AGE);

        startActivityForResult(intent, ID_AGE);
    }

    private void requestGender()
    {
        Intent intent = new Intent(getApplicationContext(), SettingsActivity.class);
        intent.putExtra(SettingsActivity.EXTRA_MODE, SettingsActivity.MODE_GENDER);

        startActivityForResult(intent, ID_GENDER);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (resultCode != RESULT_OK)
            return; //something went wrong

        switch(requestCode)
        {
            case ID_GENDER:
                int gender = data.getIntExtra(SettingsActivity.EXTRA_GENDER, -66666);
                //you've got the gender, use it
                break;

            case ID_AGE:
                //get age from the intent and use it
                break;
        }
    }
}

Upvotes: 1

Related Questions