0

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

Vivek Mishra
  • 5,669
  • 9
  • 46
  • 84
mika91
  • 53
  • 7
  • If you started application as fresh then Splashscreen->Login->MainActivity .and in between if u close the app means the activity goes in onPause() mode and get when you restart it then it will start from MainActivity onResume() mode.so in each and every activities onResume you have to Start SplashScreenActivity as per ypur apps requirement – Bhagyashri Mar 08 '18 at 10:33

1 Answers1

0

First aff all you do not need runOnUiThread if you are not calling in another thread.

runOnUiThread {
        startActivity(Intent(this, LoginActivity::class.java))
        finish()
    }

And what exactly "shop must be closed", do you have to send a request to server to close shop? If you do not have send a request then there is no need to worry about shop object when application terminates it will be cleared. But if you must clear it manually you can not determine the app killed. Because there is no callback when user swipes the app. Maybe one of the solutions can be shop closes it self if user do nothing after some minutes and reinitialize when initialize requested. Hope this helps you.

toffor
  • 1,219
  • 1
  • 12
  • 21
  • The shop must be "closed", because I need to upload transactions, close the shift, and perform some other maintenance stuffs. The flow looks like: User open the app -> go to "Init" activity -> val shop = Shop(); shop.init(); -> go to "login" or "main" activity -> close the app (menu or swipe) ->shop.close(); – mika91 Mar 15 '18 at 08:30
  • You can check this answer https://stackoverflow.com/a/26882533/7652387. An other solution can be, save last changes to shared preferences in onPause() method and when user opens the app before initing Shop() , first upload the latest one. – toffor Mar 15 '18 at 09:13