-3

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);
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
itray
  • 1
  • 1
  • You can pass data between activities through the `Intent`. Here is an example https://stackoverflow.com/a/2091482/1552587 – Titus Oct 21 '17 at 06:15
  • Thank you for comment, can i have an example on my code? – itray Oct 21 '17 at 06:20

1 Answers1

1

In MainActivity you can pass the id for recognizing into Main2Activity.
MainActivity

   public void btn_mathematics (View v)  {
        Intent intent = new Intent(MainActivity.this, Main2Activity.class);
        Bundle bundle = new Bundle();
        bundle.putString("id","Math");
        intent.putExtra(bundle);
        startActivity(intent);
    }

public void btn_physics (View v) {
    Intent intent = new Intent(MainActivity.this, Main2Activity.class);
    Bundle bundle = new Bundle();
    bundle.putString("id","Physics");
    intent.putExtra(bundle);
    startActivity(intent);
}

Main2Activity

String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    Bundle bundle = getIntent().getExtras();
    id= bundle.getString("id");
}

public void btn_semester1 (View v)
{
    if(id == "Math")
    {
        i = new Intent(this, ModulesMAT2.class);
        startActivity(i);
    }
    else if (id == "Physics")
    {
        i = new Intent(this, ModulesPHY2.class);
        startActivity(i);
    }

}


public void btn_semester2 (View v)
{
    if(id == "Math")
    {
        i = new Intent(this, ModulesMAT2.class);
        startActivity(i);
    }
    else if (id == "Physics")
    {
        i = new Intent(this, ModulesPHY2.class);
        startActivity(i);
    }

}
Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17
  • You should use `...equals(...)` to compare `String`s instead of `==` – Titus Oct 21 '17 at 06:56
  • You helped me man, thank you so much, just one little problem on my second activity, with your code: `if(id == "Math") { i = new Intent(this, ModulesMAT2.class); startActivity(i); } else if (id == "Physics") { i = new Intent(this, ModulesPHY2.class); startActivity(i); } }` i am having (cannot resolve symbole 'i') – itray Oct 21 '17 at 07:14
  • Just add Intent before each i. Like: Intent i = new Intent (this, ModulesMat2.class); – Ghulam Moinul Quadir Oct 21 '17 at 08:24