如何使多个按钮打开同一活动?

问题描述 投票:-1回答:1

我是Android和Java开发的新手,我正在尝试开发用于排序算法的应用程序enter image description here

而且我想知道如何使仪表板中的按钮打开相同的活动但使用不同的数据对于每个片段。对于所有按钮,此活动将以相同的方式工作,唯一的变化是使用的sortalgorithm.java

android android-fragments android-activity android-button
1个回答
1
投票

您可以在调用startActivity(Intent intent)方法时在意图中添加标记。是这样的:

//...
Intent intent = new Intent(this, DisplayMessageActivity.class);          
Bundle b = new Bundle();
b.putString("whatFragment","1");
intent.putExtra("extras",b);
startActivity(intent);

然后您通过以下方式将其捕获到片段中:

            /*
            .
            .
            .
             */

            Bundle b = getIntent().getExtras();
            String whatFrag=b.getString("extras");
            if(whatFrag.equals("1")){
                //code if fragment selected is 1
            }
            /*
            .
            .
            .
             */
© www.soinside.com 2019 - 2024. All rights reserved.