以编程方式定义默认活动(当应用程序启动时)

发布于 2024-11-17 21:41:03 字数 1476 浏览 2 评论 0原文

我的应用程序由一些活动组成。

活动 A 是我的主菜单,带有一些图标。 该活动可以根据您按下的图标启动:活动 B、C、D、E 或 F。

这很好,而且非常简单,活动 A 是默认的。

现在,我优先选择了一个选项,允许用户开始他们最喜欢的活动。

例如,有些用户实际上更喜欢直接获取 Activity B。

我找到解决方案的唯一方法是在 Activity A 中执行此操作。这个解决方案非常难看,因为 Activity A 总是会自动启动和关闭:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    final Intent intent = getIntent();
    String action = intent.getAction();

    if (Intent.ACTION_MAIN.equals(action)) {
        switch (Integer.valueOf(settings.getString("Activitypref", "1"))) {
        case 2:
            Intent i = new Intent(ActivityA.this, ActivityB.class);
            finish();
            startActivity(i);
            break;
        case 3:
            i = new Intent(ActivityA.this, ActivityC.class);
            finish();
            startActivity(i);
            break;
        case 4:
            i = new Intent(ActivityA.this, ActivityD.class);
            finish();
            startActivity(i);
            break;
        case 5:
            i = new Intent(ActivityA.this, ActivityE.class);
            finish();
            startActivity(i);
            break;
        case 6:
            i = new Intent(ActivityA.this, ActivityF.class);
            finish();
            startActivity(i);
            break;
        default:
            break;
        }
    } 

My application is composed by a few activity.

Activity A is my main menu with some icons.
This Activity can launch depending on which icon you press: Activity B,C,D,E or F.

That's fine and really easy, Activity A is the default one.

Now, I made an option in preference that allow users to start their favourite activity.

Some users will in fact prefer to get directly Activity B for instance.

The only way I found a solution was to do this in Activity A This solution is very ugly because Activity A will always launch and close automatically:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());
    final Intent intent = getIntent();
    String action = intent.getAction();

    if (Intent.ACTION_MAIN.equals(action)) {
        switch (Integer.valueOf(settings.getString("Activitypref", "1"))) {
        case 2:
            Intent i = new Intent(ActivityA.this, ActivityB.class);
            finish();
            startActivity(i);
            break;
        case 3:
            i = new Intent(ActivityA.this, ActivityC.class);
            finish();
            startActivity(i);
            break;
        case 4:
            i = new Intent(ActivityA.this, ActivityD.class);
            finish();
            startActivity(i);
            break;
        case 5:
            i = new Intent(ActivityA.this, ActivityE.class);
            finish();
            startActivity(i);
            break;
        case 6:
            i = new Intent(ActivityA.this, ActivityF.class);
            finish();
            startActivity(i);
            break;
        default:
            break;
        }
    } 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

下壹個目標 2024-11-24 21:41:03

考虑使用从启动器调用的包装器活动,而不是 ActivityA。您将不再需要检查 ACTION_MAIN。您还可以将目标活动名称存储在首选项中,并使用它通过不同的意图签名直接启动您的目标活动:

public Intent (String action)

 <activity class=".StartActivity" android:label="...">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
 </activity>

 <activity class=".ActivityA" android:label="...">
             <intent-filter>
                 <action android:name="mypackage.ActivityA" />                    
             </intent-filter>
 </activity>

在 StartActivity 中,

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());

    String action = settings.getString("Activitypref","mypackage.ActivityA");
    Intent intent = new Intent(action);
    startActivity(intent);
    ....
}

您可能需要稍微调整一下才能使其正确。

Instead of ActivityA, consider using wrapper activity to be called from launcher. You will eliminate a need for checking for ACTION_MAIN. Also you can store target activity name in preferences and use it to directly start your target activity via different intent signature:

public Intent (String action)

 <activity class=".StartActivity" android:label="...">
             <intent-filter>
                 <action android:name="android.intent.action.MAIN" />
                 <category android:name="android.intent.category.LAUNCHER" />
             </intent-filter>
 </activity>

 <activity class=".ActivityA" android:label="...">
             <intent-filter>
                 <action android:name="mypackage.ActivityA" />                    
             </intent-filter>
 </activity>

And in StartActivity

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    settings = PreferenceManager
            .getDefaultSharedPreferences(getBaseContext());

    String action = settings.getString("Activitypref","mypackage.ActivityA");
    Intent intent = new Intent(action);
    startActivity(intent);
    ....
}

You may need to fiddle around little bit to get it right.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文