如何让多个按钮打开同一个activity?

How to make multiple buttons open the same activity?

我是 Android 和 Java 开发的新手 我想知道如何使仪表板中的按钮打开相同的 activity 但具有不同的数据 对于每个片段。 activity 对所有按钮的工作方式相同,唯一改变的是 sortalgorithm.java 使用的

您可以在调用 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
            }
            /*
            .
            .
            .
             */