-2

(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.

a_local_nobody
  • 7,947
  • 5
  • 29
  • 51
  • Does this answer your question? [Switching between activities in android?](https://stackoverflow.com/questions/9937120/switching-between-activities-in-android) – Erfan Jul 03 '21 at 06:40
  • You shouldn't start multiple activities at the same time. Activities are meant to be shown to users. – Sonu Sanjeev Jul 03 '21 at 06:48
  • Does this answer your question? [Can someone please explain how startActivity(intent) and startActivityForResult(intent) are Asynchronous?](https://stackoverflow.com/questions/13919201/can-someone-please-explain-how-startactivityintent-and-startactivityforresult). It is being called, but you're scheduling two activities to get shown at the same time, so the latter would logically take precedence. – Henry Twist Jul 03 '21 at 07:11
  • @Erfan Though the link doesn't directly answer my question, it does give me some insights. Thank you –  Jul 03 '21 at 07:35
  • 1
    @SonuSanjeev As I mentioned in the question, this was to see how it would behave when I call two activities 'at the same time.' This isn't what I'm trying to do. It was done to solve the 'bug' I have in my program. –  Jul 03 '21 at 07:36
  • 1
    @HenryTwist That seems to make sense though shouldn't Activity B be also scheduled (and Activity C is scheduled right after B, meaning B should be called at some point since it's on a stack)? –  Jul 03 '21 at 07:38
  • check this out https://stackoverflow.com/a/9346671/12709358 , also look out https://developer.android.com/guide/components/activities/tasks-and-back-stack – Elango Jul 03 '21 at 07:47
  • 1
    @AjithkumarMuthukumaran Regarding the first link, I'm not trying to run a `Service` or `AsyncTask`. If you see my 'introduction,' you'll find out that it goes to Activity A for no reason (I don't even have code that moves from B to A and of course, I didn't press the back button). The second link is the one I have been looking at for a while as noted in my question. –  Jul 03 '21 at 07:51
  • 1
    @Droid I don't know anything about the internal workings of this, but it wouldn't make much sense for B to start as well. If another activity (C) is started before A has finished, then it would be logical to just show C. There would be no reason to maintain a 'stack' of activities if only one should be shown at a time. – Henry Twist Jul 03 '21 at 07:52
  • If you're referring to the back stack, I would guess that B doesn't even get far enough to be added, but that's just speculation. – Henry Twist Jul 03 '21 at 07:54
  • @HenryTwist After waiting for a while (15-20 minutes), it seems like B never got added (and yes, I was referring to the back stack; sorry if it was not clear). I believe you're right (though I'm still not sure as to why it's going to Activity A when I tried to move to C -- mentioned in the 'introduction'). –  Jul 03 '21 at 07:56
  • Can you clarify what you mean by that? The logs you've provided don't seem to indicate it goes back to A, unless you're referring to the `onStop` call? – Henry Twist Jul 03 '21 at 08:00
  • I should have indicated clearly that the log was for testing to understand Android lifecycles. I'll add the log for my program that has a bug. –  Jul 03 '21 at 08:05
  • Now that is interesting... Can you give a [mre] so it can be tested? – Henry Twist Jul 03 '21 at 08:27
  • @HenryTwist I tried to make a new program with minimal code but it seems like I'm not experiencing that behavior as I removed parts of code where I get fetch data from DB. Let me explain briefly what I'm doing and perhaps you can point out where I should take a closer look...? First of all, the **MainActivity** is basically a login page. I'm using PHP to fetch data from DB and check if the information is inputted correctly (by the user). Right after that, I fetch other data from DB related to that user twice; then, I move to Activity **A**. –  Jul 03 '21 at 21:49
  • + In both activities **B** and **C**, there is a `BottomNavigationView` which allows going from **B** to **C** and from **C** to **B**. As soon as I login in and move to Activity **B** via the `BottomNavigationView`, the **MainActivity** gets called (as it can be seen from the log). Other things I should mention are that in **C**, the user can move to the **MainActivity** via a menu (top right corner) by signing out, and I'm using StringRequest to fetch data. Also, there is no way to go back to the **MainActivity** (**A**) from **B** (unless you press the back button but I never did). –  Jul 04 '21 at 06:06

1 Answers1

0

After going through every line of code thrice, I found out that the problem was because I'm extending Activity A in Activity C. I don't have a very clear explanation for this but I would assume extending an activity 'creates' an 'instance'.

  • @HenryTwist I appreciate your help though. –  Jul 04 '21 at 07:49
  • 1
    Ah I see, glad you found the mistake! I can't see your code, but in your `C.onCreate` you will have called `super.onCreate`, which refers to `A.onCreate`. It's not that it `creates another instance, you're just calling the method directly. – Henry Twist Jul 04 '21 at 08:37
  • 1
    That makes sense perfect sense! Thank you again. –  Jul 04 '21 at 08:38