So, I've been trying to really double down on my Android development learning lately. I start my first software dev job next month, and I feel like I should get some sort of real experience under my belt, not just basic implementations of data structures and algorithms in isolated C++ projects.
I picked up this "Quotr" app of mine that I started working on early last year.
It was pretty basic, basically just displayed Quotes, had some fun animations implemented, etc. Every once in awhile I'd throw in a little tweak or something, but now I kinda want to make it into a proper, usable thing, so I've been implementing database storage with SQLite so the app saves data between launches, I added a RecyclerView to see the quotes you have stored, today I made it so you can select one to jump to it, and I'm planning on adding editing/deleting soon.
ANYWAY, I've been having some serious issues with the lifecycle of the activities in my app. Particularly, I was having startup/cleanup code running at really weird points in my app's lifecycle. The first new activity I added to the app was the one that allowed you to add a quote. So, seeing as it was the first ever new activity I added, I didn't really know what I was doing yet. Fast forward to now where I'm a bit better, and I didn't catch this glaring mistake.The problem I was having:
Whenever I'd launch the AddQuote activity, onCreate() would be called from my main activity. When I finished the AddQuote activity, onDestroy() of my main activity would be called. This was causing big issues, because I closed my database connection and reset the cursor that kept track of where you were in the quote list in the onDestroy() method. And it's just generally bad that onCreate() was being called multiple times because it's a waste of resources.What was causing it:
Turns out, since I had no idea what I was doing, I made the child activity extend the main/parent activity. Really, I should have it extending AppCompatActivity, but extending the main class worked, so that's what I did. The issue came in when I was doing the initialization and destruction of the child class, I'd make a call to the super.onCreate() and super.onDestroy() like you're supposed to do, but because I was extending the parent class, the super method was the main activity's onCreate() and onDestroy() which made absolutely no sense to be executing at those points in time. I'm honestly surprised I was able to work with this for so long before figuring it out. But I'm so glad that I did finally figure it out, because things actually work like I intended them to now.