I'm a newbie in android dev, and after a couple of hours of tests, documentation and forum readings, I still can't manage and understand how to deal with android lifecycle
Let's see my app's activity flow (quite classic I guess): splash -> login -> main
During splash screen, I need to instantiate and start a 'Shop' object, that will be global to my app and use in all other activities. The start is quite long (around 10 seconds) because is need server authentication, data downloading and data pre-processing.
Whenever the application is 'closed', it should be removed from recents app and the shop must be closed (client requirement) If the app is restarted, need to go back to splash activity and re-instantiate the shop
For now, what I firstly implemented looked like this:
// splash activity
private fun goToLogin() {
runOnUiThread {
startActivity(Intent(this, LoginActivity::class.java))
finish()
}
}
// login activity
private fun goToMain() {
runOnUiThread {
startActivity(Intent(this, MainActivity::class.java))
finish()
}
}
// in app class
fun quit(ctx: Activity) {
ctx.finishAndRemoveTask()
// close shop and kill app
shop.close()
android.os.Process.killProcess(android.os.Process.myPid())
}
It's not working as expected: First, it's not an android goood practice: the OS should manage itself applications shutdown. Second, even if application is closed, another one seems to be immediatly started after previous being killed. I added the following code to demonstrate:
// app onCreate()
override fun onCreate() {
super.onCreate()
Thread {
while (true) {
Thread.sleep(1000)
log.info("APP RUNNING")
}
}.start()
}
and I continue to have "APP RUNNING" in my logs after application exit. (but in another Thread pid)
So I just tried to remove killProcess() and let the app continue leaving. The problem is that now, when I restart the app, I directly go to 'login' activity, instead of splash one. Don't know why, my manifest seems correct and on fresh start, I go to splash activity as expected.
<activity
android:name="com.thalesgroup.dk.mark.app.activities.SplashActivity"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.thalesgroup.dk.mark.app.activities.LoginActivity"
android:screenOrientation="portrait" />
<activity
android:name="com.thalesgroup.dk.mark.app.activities.MainActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme.NoActionBar" />
So if you have any comments on android good practice and way to go for my app, I would appreciate the help! Thanks