0

I created another class where I have function of logout:

fun logOut(context: Context) {    
        context.stopService(Intent(context, CheckNewMessages::class.java))
        val intent = Intent(context, LoginScr::class.java)
        intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP and
                Intent.FLAG_ACTIVITY_NEW_TASK and
                Intent.FLAG_ACTIVITY_NO_ANIMATION
        context.startActivity(intent)

        (context as Activity).finish()
    }

and as you can see I use this line for finishing activity:

(context as Activity).finish()

But it is still alive and as a result I have two or more same activities at my system. I tried a lot of ways like creating static variable at first activity and using this variable at the second one for closing. But my activity stays alive. I also tried to use lauchmode at manifest and some other ways. Maybe someone knows where I did a mistake?

UPDATE

Two places from which I call logOut(). 1st is interface between RV adapter and fragment:

override fun finish() {
        APICallRequests.logOut(context!!)
        activity!!.finishAffinity()
    }

and 2nd at Interceptor for requests:

private fun updateAccessToken(context: Context) {
        val sp = context.getSharedPreferences(Constants.SHARED_PREFS_STORAGE, 0)
        synchronized(this) {
            val tokensCall = accessTokenApi()
                    .getNewToken(ReqAccessToken(sp.getString("refresh_token", "")!!))
                    .execute()

            if (tokensCall.isSuccessful) {

            } else {
                when (tokensCall.code()) {
                    500 -> {
                        val thread = object : Thread() {
                            override fun run() {
                                Looper.prepare()
                                Toast.makeText(cont, cont.getString(R.string.server_error_500), Toast.LENGTH_SHORT).show()
                                Looper.loop()
                            }
                        }
                        thread.start()
                    }

                    401 -> {
                        APICallRequests.logOut(context)
                    }
                }
            }

        }
    }
Andrew
  • 1,947
  • 2
  • 23
  • 61
  • What calls the function `logOut()` and when the function is called, what is passed in as `context`? – David Wasser Apr 16 '20 at 15:21
  • @DavidWasser, in general it can be called as user presses logout btn, and also from interceptor when the user receives some errors also, I pass fragments as context – Andrew Apr 16 '20 at 15:30
  • I still don't understand your problem. Anyway, you cannot pass a `Fragment` as a `Context` parameter because `Fragment` doesn't extend `Context`. The compiler would flag this as an error so you are obviously not passing a `Fragment` as `context`. – David Wasser Apr 16 '20 at 20:11
  • Please post the code that calls `logOut()`. – David Wasser Apr 16 '20 at 20:12
  • @DavidWasser, I have just updated my question, check it pls :) – Andrew Apr 17 '20 at 07:18
  • Thanks, but that still doesn't help. I need to know what `context` is. How is the value of `context` set? – David Wasser Apr 17 '20 at 10:27
  • I pass it from fragment class, at interceptor it passed at constructor from non acitivity class – Andrew Apr 17 '20 at 10:34
  • But where do you get the `context` from? – David Wasser Apr 17 '20 at 13:34
  • Please also try this: kill your app (force quit from the settings). Now launch your app from the HOME screen. now do whatever you do and see if you still have 2 copies of the `Activity`. If you don't, then you are just experiencing this nasty long-standing Android bug: https://stackoverflow.com/a/16447508/769265 – David Wasser Apr 17 '20 at 13:37

2 Answers2

1

try to pass Activity instead of Context in inner param fun logOut(activity: Activity), this should help you if you are calling this function from activity. If you calling it from fragment you can use requareActivity.finish()

i30mb1
  • 3,894
  • 3
  • 18
  • 34
1

That's not the way it works. What happens is this. When you do:

context.startActivity(intent)

This doesn't start the new Activity immediately. It just requests that Android starts the new Activity when it gets control back. Then you do this:

(context as Activity).finish()

This call just finishes the current Activity. When you eventually return control to the Android framework, it will launch the new Activity as you requested in your call to startActivity().

If you want your app to exit (ie: all activities finished), you can just do:

(context as Activity).finishAffinity()

This call will finish the current Activity and all other activities in the task that belong to the same app.

NOTE: This only works if all activities in your app share the same affinity, which is the default case.

David Wasser
  • 93,459
  • 16
  • 209
  • 274
  • but the current acitivity can be closed via `.finish()` also? – Andrew Apr 16 '20 at 14:43
  • It isn't necessary because `finishAffinity()` will close all of them, including the current one. – David Wasser Apr 16 '20 at 14:49
  • but if I finish all activities so my app will be closed, not like `System.exit(0)` but I will be moved on home screen? as for what I want I would like to close activity and open another one, for ex close A and move to B, using `finishAffinity()` will allow me to enter B? – Andrew Apr 16 '20 at 14:53
  • Sorry. I do not understand your problem then. Do you just want to start a new `Activity` and finish the current one? – David Wasser Apr 16 '20 at 15:19