I'm currently writing a sports app which is able to keep track of a scoreline. The user is supposed to choose a sport in an activity called "ChooseSport" because I am planning to include features specific for different sports. After his choice he is directed to an activity where he is supposed to specify the team names and finally to the activity where he can change the score by submitting user input every time a goal occurs. Depending on his choice in the first activity, both of the subsequent activitites need to be different. But they should still have many similarities.
I have therefore decided to set up everything in the following way: "ChooseSport" starts an intent to open the activity containing the EditTexts for the team names dependent on the sport, such as "FootballActivity", "HandballActivity", "WaterpoloActivity". All these activites inherit from the more generic class "SportActivity". The third activity containing the scoreline is treated similarly: there are activities such as "FootballScoreActivity", "HandballScoreActivity" and "WaterpoloScoreActivity" which all inherit from the more generic class "ScoreActivity". If you pick "football" in "ChooseSport" you are supposed to be directed to "FootballActivity" and "FootballScoreActivity" afterwards. So depending on the choice of sport not only the intent to get from "ChooseSport" to the second activity, but also the one to get from the second activity to the third one changes. I have tried to implement this behaviour using static variables in "SportActivity":
public class SportActivity extends AppCompatActivity {
public static String titleToolbar;
static ScoreActivity scoreActivityInstance;
...
Intent intent = new Intent(this, streamActivityInstance.getClass());
...
startActivity(intent);
}
public class FootballActivity extends SportActivity {
static {
titleToolbar = "Football";
scoreActivityInstance = new FootballScoreActivity();
}
}
( I'm using .getClass()
on an instance instead of .class
on the class because I haven't found a way to reference the correct class in SportActivity depending on the decision in "ChooseSport".)
This seemed to work fine at first but after some testing I have spotted a serious bug:
If I open a FootballScore starting from ChooseSport, go back pushing the back button twice, open a HandballScore, go back again, and open a FootballScore again, I end up having a HandballScore!
I suspect the following thing to happen: when starting the first FootballActivity, the static variables in the superclass SportActivity are overwritten, because the FootballActivity is actually initialised. Starting the first HandballActivity, the static variables of SportActivity are again overwritten, because the HandballActivity is actually initialised. Starting a FootballActivity for the second time however doesn't seem to overwrite the static superclass variables, because again a HandballScore is started so that apparently the superclass variables had still been in the "handball mode". I assume this might be because there is already an instance of FootballActivity so android does not instantiate FootballActivity again resulting in the static variables of the superclass to not change. The problem seems quite difficult to me because android is instantiating FootballActivity and HandballActivity on its own and I do not have full insight of how android is doing this.
To fix the bug I have tried to call finish()
after startActivity(intent)
in SportActivity. I expected the activity to be destroyed since finish()
is supposed to trigger a call to onDestroy()
. Nevertheless, this did not fix the bug, and reading a bit further I found out that finish()
just results in onDestroy()
being invoked but not necessarily in the instance of the activity being destroyed (see the ticked comment here).
So I'd be very thankful for any hints how to fix my bug, in particular:
1.) Do you agree that my general approach makes sense?
2.) Even if so, can you come up with a way which does not lead to such difficulties?
3.) Do you agree on my assumptions why the bug occurs?
4.) What can I do to fix the bug?
Thanks for any well-meant piece of advice.