How to make multiple buttons open the same activity?

别等时光非礼了梦想. 提交于 2020-05-24 07:31:13

问题


I am new to Android and Java development and I am wondering how to make the buttons in the dashboard open the same activity but with different data for each fragment . this activity will work the same way for all the buttons the only thing changing is the sortalgorithm.java used


回答1:


you can put a flag in your intent when you call startActivity(Intent intent) method. is something like this:

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

Then you catch this in your Fragment with:

            /*
            .
            .
            .
             */

            Bundle b = getIntent().getExtras();
            String whatFrag=b.getString("extras");
            if(whatFrag.equals("1")){
                //code if fragment selected is 1
            }
            /*
            .
            .
            .
             */


来源:https://stackoverflow.com/questions/61071427/how-to-make-multiple-buttons-open-the-same-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!