I’m not getting to finish the Activitys

Asked

Viewed 55 times

0

My problem is this... I have 3 Activitys:

(Activitya), (Activityb) and (Activityc)

From Activitya to Activityb I do the following:

    val intent = Intent(this, ActivityB::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
    startActivity(intent)
    finish()

from Activityb to Activityc I also do the same thing:

    val intent = Intent(this, ActivityC::class.java)
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)

    intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
    startActivity(intent)
    finish()

It turns out that if I give onBackPressed or go to any other App and return to my App instead of going back to Activityc as expected I always return to Activitya!!!!!!

1 answer

1

The flag Intent.FLAG_ACTIVITY_NO_HISTORY makes sure Activity doesn’t get into the history stack. So when changing between apps and back Android does not have the history of which Activity was open in the app and restarts the app, calling the Activity Launcher again which in your case should be Activitya.

And the flags Intent.FLAG_ACTIVITY_CLEAR_TOP and Intent.FLAG_ACTIVITY_NEW_TASK will clear the history stack of all previously opened Activity. So by pressing onBackPressed instead of going back to last opened Activity, will close the app completely, and when coming back Android will open from Activity Launcher.

If you want even closing the app it goes back to the Activity it was previously, you need to create a control, for example, save in Sharedpreferencs where the user was, and in Activity Launcher redirect.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.