Question newcomer to android. I started to study intents: I want to attach 3 buttons to 3 layout, so that in the second activit we could see which button was pressed and the corresponding layout was called. So generally do? And how to do it correctly through intent?
Activity_1.java:
public void onClick(View v){ switch (v.getId()){ case R.id.button_1: { Intent intent = new Intent (getApplicationContext(),Activity_2.class); intent.putExtra("key","b1"); startActivity(intent); break; } case R.id.button_2: { Intent intent = new Intent (getApplicationContext(),Activity_2.class); intent.putExtra("key","b2"); startActivity(intent); break; } case R.id.button_3: { Intent intent = new Intent (getApplicationContext(),Activity_2.class); intent.putExtra("key","b3"); startActivity(intent); break; } Activity_2.java:
@Override protected void onCreate (Bundle savedInstanceState) { super.onCreate (savedInstanceState); String data = getIntent().getStringExtra("key"); if (data == "b1") setContentView (R.layout.activity_number1); else if (data == "b2") setContentView (R.layout.activity_number2); else setContentView (R.layout.activity_number3); }