(Sorry for the wordy introduction.)
I have a 'bug' in my program where it randomly goes to an activity when it shouldn't. For example, let's say I have activities A (the MainActivity), B, and C. I moved from A to B and then B to C. However, while moving from B to C, I noticed that it goes from B to A and then A to C (which makes no sense to me; why does it not even 'come back' to B?). I wasn't able to find out why since I do not have any code that goes to A from B.
Here's the log:
A onCreate (from: None) // Just started the program
A onStart
A onResume
Clicked a button from Activity A to move to B
A onPause
B onCreate (from: A)
B onStart
B onResume
A onStop
A onDestroy
Clicked a button from Activity B to move to C
B onPause
A onCreate (from: B) // Why?
C onCreate (from: B)
A onStart
C onStart
A onResume
C onResume
B onStop
B onDestroy
Although I believe I'm not moving to two activities 'at the same time,' I thought I am since A gets called. In order to verify this and see what happens if I move to two different activities 'at the same time,' I made a simple program that moves to B and C from A like this:
startActivity(new Intent(this, B.class));
startActivity(new Intent(this, C.class));
Once I press a button in A, these two get called. I have overridden onCreate()
, onStart()
, onResume()
, onPause()
, onStop()
, onRestart()
, and onDestroy()
for each activity to put print
statements to see when each of them is called. This was the result once I pressed the button in Activity B (to move to Acitivity C).
A onCreate
A onStart
A onResume
A onPause
C onCreate
C onStart
C onResume
A onStop
What I don't understand is why is B not being called (not even onCreate()
). Is it because it executes the following command right after that there is no 'enough time' to even call onCreate()
for B? I have been looking at this and this (and SO sites) for a while, but I don't think I understand this behavior.
Is there a reason why B is not being called? Also, what could be a reason why it's moving to Activity A from B and then B to C (mentioned in the 'introduction')?
EDIT: To be clearer, I'm not trying to run two activities at the same time. It's the fact that (from the 'introduction') Activity A got called for no reason when I tried to move to Activity C. I was thinking this could be because of Activity A not being destroyed (still on the stack) though I'm not sure what the problem is.
EDIT 2: I tried to come up with a minimal reproducible example by creating a new program, but it seems like I'm not experiencing the behavior probably because I removed fetching data from DB (perhaps it's because of this?). I briefly explained what I'm doing in the comments below.