简体   繁体   中英

One activity with two successor activities

On my main activity(start window of app) I have Button and Button_1. It doesn't matter what will click here the user will open Activity_1 . Activity_1 is the same for both buttons. The different will be from Activity_1 depends which button is clicked must open Activity_2 or Activity_3.

Here is the scenario.

Button   -> Activity 1 -> Activity 2
Button_1 -> Activity 1 -> Activity 3

I have everything done but the part with knowing which button is clicked.

Button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, Menu.class);
    startActivity(intent);

}
});
Button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, Menu.class);
    startActivity(intent);

}
});

Menu.class is listview with image and text. If Button is clicked and open listview user is able to click on item and read some information. If Button_1 is clicked it load same listview and when user click on item should open different info from another activity.

Currently I'm able to successfully open Menu.class . There I load my listview and I can pass the info to next activity but just to one..

How can I store/know which button is pressed so according to this to load proper activity. Whit IF ? case ?

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);

    boolean isButton = getIntet().getExtra("isButtonClicked",false);
    if(isButton)
    {
     Intent intent = new Intent(Menu.this, Information.class);

        startActivity(intent);
    }
    else
    {
     Intent intent = new Intent(Menu.this, Information1.class);

        startActivity(intent);
    }
    // Download JSON File   
    new DownloadJSONFileAsync().execute();

}

Pass button clicked information to menu activity by intent.putExtra() method like below

Button.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, Menu.class);
    intent.putExtra("isButtonClicked",true);
    startActivity(intent);

}
});
Button1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
    Intent intent = new Intent(MainActivity.this, Menu.class);
    intent.putExtra("isButtonClicked",false);
    startActivity(intent);
}
});

and on Menu Activity onCreate() method get the clicked button by

 boolean isButton = getIntent().getBooleanExtra("isButtonClicked",false);

if isButton value is true then button is clicked. otherwise button1 is clicked.then start new activity by

   if(isButton)
{
 Intent intent = new Intent(Menu.this, Activity2.class);

    startActivity(intent);
}
else
{
 Intent intent = new Intent(Menu.this, Activity3.class);

    startActivity(intent);
}

You can easily use intent.putExtra("value", true); and in Activity 1 onCreate method

boolean val = getIntent().getBooleanExtra("value", false);

and based on val go to either Activity 2 or 3.

You can use extras:

Bundle bundle = new Bundle();
bundle.putInt("btn", 1);

Intent menu = new Intent(this, Menu.class);
menu.putExtras(bundle);

startActivity(menu);

In another activity:

@Override
public void onCreate(Bundle b){
   Bundle myBundle = getIntent().getExtras();
   if (myBundle.getInt("btn") == 1){
      // todos
   } else if (myBundle.getInt("btn") == 2){
      // todos 
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM