After much work, I am nearing the next version release of my app. While putting it through its paces (i.e. trying to break it!) I stumbled on a somewhat rare and hard to reproduce error.
I believe the problem is stemming from the fact that when UserClosed = True on my main (starting) activity, I am killing it with ExitApplication (despite all of the posts here by Erel and several others saying not to do that). I know it is normal to see a process "running" even after closing it but I just don't like to see that there.
Trying to reproduce this is difficult but I believe it has something to do with Android recreating a process in my app. Below is how to create the error, but it does not happen every time and it takes many many tries:
1) Start the app
2) Start another activity in the app i.e. StartActivity ("Setup")
3) Press the Home button to essentially pause the app
4) Go back to my app either by holding Home and choosing it or pressing on its shortcut/icon
5) 99% of the time, it resumes where it left off. On a rare occasion, it instead starts the app on the main screen as if it is the first time I ran it.
6) When the app fails to resume where I left off but instead appears to start over, if I then press Back, which calls ExitApplication, I get a NullPointerException error in the last activity I was in when I pressed Home. If I tell it to continue anyway it goes through a long series of NPE errors from just about every activity and sub in my app!
I suspect that ExitApplication is my problem, but since it is difficult to test, I was hoping to hear from someone that can tell me (if this is the cause), why is this happening and only occasionally? For all I know there is something else wrong in my app but other than this goofy string of events that leads up to it, it seems solid.
If I do need to remove ExitApplication, then I have another dilemma. I use the network library to detect WiFi. It is declared in Process_Globals:
Then initialized in Activity_Create (FirstTime = True):
I have been closing that socket when the user closes the app (before calling ExitApplication). If I can no longer ExitApplication, where should I close the socket? Or should I open and close before/after I want to check the connection? Maybe open it where I have it in Activity_Create and then close it at the end of the sub GetNetInfo (which is where it determines the IP address of the device)? The reason why I didn't do this initially is because I was afraid it would close the socket before it got a chance to determine the IP address. Should I move the initialize stuff from Activity_Create to Activity_Resume?
There is a lot of good and helpful info here but I'm still a bit off my game in Android land. While I think my app is running pretty smoothly (except for this problem), sometimes I feel like it is nothing more than bits and pieces of disjointed code that may or may not be in the correct spot (Activity_Create as opposed to Resume, FirstTime=True or not, etc). This feeling was made worse by my recent update which allows both orientations for the first time and I had some issues with data loss initially. Reading the forum here helped me get it going but it was more like, "Okay. That didn't work, so maybe I will try this!" It kind of reminds me of way back when I started Windows programming (VB3) and did a lot of experimenting to get things to work right because it was new to me.
I believe the problem is stemming from the fact that when UserClosed = True on my main (starting) activity, I am killing it with ExitApplication (despite all of the posts here by Erel and several others saying not to do that). I know it is normal to see a process "running" even after closing it but I just don't like to see that there.
Trying to reproduce this is difficult but I believe it has something to do with Android recreating a process in my app. Below is how to create the error, but it does not happen every time and it takes many many tries:
1) Start the app
2) Start another activity in the app i.e. StartActivity ("Setup")
3) Press the Home button to essentially pause the app
4) Go back to my app either by holding Home and choosing it or pressing on its shortcut/icon
5) 99% of the time, it resumes where it left off. On a rare occasion, it instead starts the app on the main screen as if it is the first time I ran it.
6) When the app fails to resume where I left off but instead appears to start over, if I then press Back, which calls ExitApplication, I get a NullPointerException error in the last activity I was in when I pressed Home. If I tell it to continue anyway it goes through a long series of NPE errors from just about every activity and sub in my app!
I suspect that ExitApplication is my problem, but since it is difficult to test, I was hoping to hear from someone that can tell me (if this is the cause), why is this happening and only occasionally? For all I know there is something else wrong in my app but other than this goofy string of events that leads up to it, it seems solid.
If I do need to remove ExitApplication, then I have another dilemma. I use the network library to detect WiFi. It is declared in Process_Globals:
B4X:
Dim MyLan As ServerSocket
Then initialized in Activity_Create (FirstTime = True):
B4X:
MyLan.Initialize(0, "") ' For getting WiFi status / IP
If MyLan.IsInitialized Then GetNetInfo
I have been closing that socket when the user closes the app (before calling ExitApplication). If I can no longer ExitApplication, where should I close the socket? Or should I open and close before/after I want to check the connection? Maybe open it where I have it in Activity_Create and then close it at the end of the sub GetNetInfo (which is where it determines the IP address of the device)? The reason why I didn't do this initially is because I was afraid it would close the socket before it got a chance to determine the IP address. Should I move the initialize stuff from Activity_Create to Activity_Resume?
There is a lot of good and helpful info here but I'm still a bit off my game in Android land. While I think my app is running pretty smoothly (except for this problem), sometimes I feel like it is nothing more than bits and pieces of disjointed code that may or may not be in the correct spot (Activity_Create as opposed to Resume, FirstTime=True or not, etc). This feeling was made worse by my recent update which allows both orientations for the first time and I had some issues with data loss initially. Reading the forum here helped me get it going but it was more like, "Okay. That didn't work, so maybe I will try this!" It kind of reminds me of way back when I started Windows programming (VB3) and did a lot of experimenting to get things to work right because it was new to me.