I am new to Android, and i am trying to make a one button open 2 activities but is not working for me.
for ex:
on Mainacitivity
, there is btn_mathematics
and btn_physics
open the same activity (Main2acitivity
) and find btn_semester1
and btn_semester2
, each button will open 2 other activities for the semester modules.
If the user on Mainacitivity clicked on:
btn_mathematics
---> btn_semester1
---> will have ModulesMAT
and if clicked on the btn_semester1 same button:
btn_physics
---> btn_semester1
---> will have ModulesPHY
.
MainActivity XML:
<Button
android:id="@+id/btn_mathematics"
android:onClick="btn_mathematics"
android:text="@string/btn_mathematics/>
<Button
android:id="@+id/btn_physics"
android:onClick="btn_physics"
android:text="@string/btn_physics"/>
Main2Activity XML:
<Button
android:id="@+id/btn_semester1"
android:onClick="btn_semester1"
android:text="@string/btn_semester1"/>
<Button
android:id="@+id/btn_semester2"
android:onClick="btn_s2"
android:text="@string/btn_semester2"/>
I guess that no need to add xml for ModulesMAT and ModulesPHY, its pretty similar to the the others.
and now the java code:
MainActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void btn_mathematics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
`public void btn_physics (View v) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
Main2Activity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
}
public void btn_semester1 (View v)
{
Intent i = getIntent();
String id = i.getStringExtra("id");
if(id == "btn_mathematics")
{
i = new Intent(this, ModulesMAT.class);
startActivity(i);
}
else if (id == "btn_physics")
{
i = new Intent(this, ModulesPHY.class);
startActivity(i);
}
}
public void btn_semester2 (View v)
{
Intent i = getIntent();
String id = i.getStringExtra("id");
if(id == "btn_mathematics")
{
i = new Intent(this, ModulesMAT2.class);
startActivity(i);
}
else if (id == "btn_physics")
{
i = new Intent(this, ModulesPHY2.class);
startActivity(i);
}
}