FutureShocked FutureShocked - 1 month ago 19
Android Question

Use the app selection interface to choose different ways to launch activity

I'd like to use the app selection interface to change starting values in an activity. The app selection interface I'm referring to is the one that pops up when you have multiple apps to catch the same type of intent, and allows the user to choose one as well as always use that selection.

The activity in question currently displays a button for the selection of two choices, builds an intent based on that, fires the intent, and exits. That's it. Instead of the custom built button, I'd like to use the above stated interface to display the choices, as the majority of the interface will retain the user's chosen language. Ideally, this would be used to just set a value in the Activity's class. The two selections would need to have different names, in this case term/xterm.

Is there a way to accomplish this?

final Intent intent = getLaunchIntent();

//Would like to replace this Alert Dialog with the app selection interface
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.launch_preference);
builder.setPositiveButton(R.string.term_preference, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
intent.putExtra("launchType", "launchTerm");
dialog.dismiss();
startActivity(intent);
finish();
}
});
builder.setNegativeButton(R.string.xterm_preference, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
intent.putExtra("launchType", "launchXTerm");
dialog.dismiss();
startActivity(intent);
finish();
}
});
builder.create().show();
builder.create().dismiss();

Answer

I succeeded in this by doing the following:

If the intent that creates my main activity comes from being launched, I send a custom intent that is caught by two other activities in the app. This allows for a selection between the two. The text that is displayed can be set by putting an android:label element in the opening intent-filter.

Each of those activities then sends an intent to the main activity with an extra determining launch type, and execution can continue as normal.

    <activity android:name=".Activity1" >
        <intent-filter android:label="text to be displayed in selection" >
            <action android:name="SELECTION_INTENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name=".Activity2" >
        <intent-filter android:label="text to be displayed in selection" >
            <action android:name="SELECTION_INTENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

    <activity android:name="MainActivity" >
        <intent-filter>
            <action android:name="CHOICE_INTENT" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>