0

I have HomeScreen and LoginScr activity and I would like to move from HomeScreen to LoginScr activity. I do it in such way:

val thread = Thread(Runnable {
    try {
     val client = APICallRequests.client
     client.dispatcher.executorService.shutdown()
     client.connectionPool.evictAll()
    } catch (e: java.lang.Exception) {
     e.printStackTrace()
    }
})

thread.start()


val intent = Intent(this, LoginScr::class.java)
startActivity(intent)
finishAfterTransition()

As you can see I try to close okHttp3 client, because maybe it keeps HomeScr activity alive. Also I use finishAfterTransition(). Then when I move from LoginScr to HomeScr I do it in such way:

private fun moveToNextScreen(){
        val goToMain = Intent(this, HomeScreen::class.java)
        startActivity(goToMain
                .addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
        )
        finishAfterTransition()
        overridePendingTransition(0, 0)
    }

As I see I create two activity instances instead one. How I did it - added log to onCreate() fun. I totally don't understand why does it happen. Similar problem can be met after onBackPressed():

override fun onBackPressed() {
   val startMain = Intent(Intent.ACTION_MAIN)
   startMain.addCategory(Intent.CATEGORY_HOME)
   startMain.flags = Intent.FLAG_ACTIVITY_NO_ANIMATION
   startActivity(startMain)
   finish()
   super.onBackPressed()
}

I tried a lot of variants but my HomeScreen can stay alone. I also tried such ways at manifest:

android:launchMode="singleTask"
android:noHistory="true"

Maybe someone know how to solve this problem?

Andrew
  • 1,947
  • 2
  • 23
  • 61

1 Answers1

0

I think this answer can help you https://stackoverflow.com/a/10862977/13221053 Further, if you want to keep your app active in background then you can use services for that purpose, here is the documentation https://developer.android.com/guide/components/services