How to make multiple buttons open the same activit

2020-05-09 19:31发布

问题:

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
            }
            /*
            .
            .
            .
             */