user1710911
user1710911

Reputation: 675

User choose which Activity start first?

I need help please with User choose which Activity launch first....

I have 3 activities and settings activity, i want the user choose an activity to launch first and save.

I will appreciate if anybody guide me to the right code or example...

<activity
    android:label="@string/app_name"
    android:screenOrientation="portrait"
    android:name="com.example.Test" 
    android:theme="@style/Theme.FullScreen">
    <intent-filter >
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

when i start an app always start with Test.class how do i set by user choose which one start?

Thanks in advance..

Upvotes: 1

Views: 448

Answers (2)

yahya.can
yahya.can

Reputation: 1810

Create temp activity. In this activity , user can choose activity name. Save selected activity name(with this param "activity")(For example; FirstActivity,SecondActivity,ThirdActivity).

getSharedPreferencesStringValue =>write this func

Algorith And control it in temp activity(at onCreate func.).

1-If there is a value which name is "activity", then again control it.

 1.1-If getSharedPreferencesStringValue("activity").equals("FirstActivity")

  -Redirect FirstActivity
 1.2-If getSharedPreferencesStringValue("activity").equals("SecondActivity")

  -Redirect SecondActivity
 1.2-If getSharedPreferencesStringValue("activity").equals("ThirdActivity")

  -Redirect ThirdActivity

2-If there is not a value which name is "activity"

 2.1-Open temp activity

 2.2-Show activity list

 2.3-After than user chose an activity, save it preferences

 2.4-And open that activity

Best regards,

Upvotes: 0

Andrew T.
Andrew T.

Reputation: 4707

Create an Activity as the main entry-point for your app. This activity will check whether the user has chosen the default activity or not. If he has, then this activity will launch that activity and close itself. Else, it will prompt the user to choose the default activity.

Sample code (you need to make some modifications to suit your requirement):

public class MainActivity extends Activity {

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

        final SharedPreferences sharedPref = getPreferences(MODE_PRIVATE);
        int choice = sharedPref.getInt("default_activity", -1);

        if (choice == -1) {
            // show the option to choose the default activity to the user
            // e.g. dialog with list, then save the corresponding choice to
            // shared preference
            String[] activities = { "Activity 1", "Activity 2", "Activity 3" };

            AlertDialog.Builder builder = new Builder(this);
            builder.setAdapter(
                new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, activities),
                new OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    SharedPreferences.Editor editor = sharedPref.edit();
                    editor.putInt("default_activity", which);
                    editor.commit();
                    launchActivity(which);
                }
                }).show();
        } else {
            // start the activity and close this activity
            launchActivity(choice);
        }
    }

    private void launchActivity(int choice) {
        switch (choice) {
        case 0:
            startActivity(new Intent(this, Activity1.class));
            break;
        case 1:
            startActivity(new Intent(this, Activity2.class));
            break;
        case 2:
            startActivity(new Intent(this, Activity3.class));
            break;
        }
        finish();
    }
}

Upvotes: 4

Related Questions