0

So i made a homepage for my app (activity_home.xml / HomeActivity.java) and added 4 buttons , each buttons takes the user to a specific activity, so far 2 are working fine, but the last two either reload the home activity or restart the app.

the 2 activities i'm talking about are named (yoga3.xml / activity_yoga3.java) and (cardio2.java /activity_cardio2.xml).

activity_home.xml :

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/buttonn3"
    android:layout_width="300dp"
    android:layout_height="79dp"
    android:layout_below="@id/buttonn2"
    android:layout_marginLeft="60dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="2dp"
    android:background="@android:color/transparent"
    android:fontFamily="@font/airstrike"
    android:onClick="cardio2"
    android:text="@string/your_string3"
    android:textColor="@color/design_default_color_error"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent">


</androidx.appcompat.widget.AppCompatButton>

<androidx.appcompat.widget.AppCompatButton
    android:id="@+id/buttonn4"
    android:layout_width="300dp"
    android:layout_height="79dp"
    android:layout_below="@id/buttonn3"
    android:layout_marginLeft="60dp"
    android:layout_marginTop="10dp"
    android:layout_marginBottom="2dp"
    android:background="@android:color/transparent"
    android:fontFamily="@font/airstrike"
    android:onClick="yoga3"
    android:text="@string/your_string4"
    android:textColor="@android:color/holo_green_light"
    android:textSize="30dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent">

</androidx.appcompat.widget.AppCompatButton>

HomeActivity.java :



Button buttonn = (Button) findViewById(R.id.buttonn1);
Button buttonn2 = (Button) findViewById(R.id.buttonn2);
Button buttonn3 = (Button) findViewById(R.id.buttonn3);
Button buttonn4 = (Button) findViewById(R.id.buttonn4);



buttonn.setOnClickListener(v -> {
Intent intent = new Intent(this,WaterActivity.class);
startActivity(intent);
});

buttonn2.setOnClickListener(v -> {
    Intent intent = new Intent(this,Bodybuilding.class);
    startActivity(intent);
});

buttonn3.setOnClickListener(v -> {
    Intent intent = new Intent(this,cardio2.class);
    startActivity(intent);
});

buttonn4.setOnClickListener(v -> {
    Intent intent = new Intent(this,yoga3.class);
    startActivity(intent);
});

i changed the buttons to another activity and it worked, but when i change it back to yoga3 and cardio2 it doesn't work

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254

1 Answers1

0

Congrats on your first question! Sounds like an annoying problem, hope that some of the below will help.


Based on the limited info, my guess would be the onClick attribute in XML. That should be a method name with specific signature on the activity that called setContentView. Those functions may be messing with you, or crashing because they don't exist? (Ref How exactly does the android:onClick XML attribute differ from setOnClickListener?)

A crash can manifest in quick restart, but you said there's no errors in logcat. If I were you, I would double check that, making sure to remove all filters and enable verbose logging.


Even if no errors are hiding behind the filter, you should find ActivityManager logs (not sure of the exact log tag) stating which activities are being launched when you click, or simply giving warnings or debug info that should be getting you closer to the solution.


Also double check if there are duplicate +id/ definitions for these IDs, maybe findViewById is finding a different view than you expect?


If these don't help, please update the question with all 4 activity launches, the view XML for each, and the code that doesn't work in its exact form, and additionally please show the AndroidManifest.xml entries for each of the activities in play main and 4 launched), because singleTop or similar flags could be messing with launches too.

TWiStErRob
  • 44,762
  • 26
  • 170
  • 254