Combine flags and clear back trace in kotlin

5.4k Views Asked by At

I am using Kotlin to develop an android application. In Android, we were using addFlags and setFlags to set flags and clear the backtrace activities separated by "|". How to achieve it in Kotlin? I am currently using below code in Kotlin:

startActivity(Intent(context, MyActivity::class.java)
            .setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK))

But it is not Working. I have also tried addFlags, It is also not working. What should be done in this scenario?

5

There are 5 best solutions below

4
On BEST ANSWER

Use it like this

val intent = Intent(context, MyActivity::class.java)                
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
0
On

use this with intent in order to clear the backtrace activities

var intent = Intent(this, [Your_activity]:class.java)
    intent.flags =  Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
    startActivity(intent)
0
On

use this with intent in order to clear the backtrace activities

Java

intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);

Kotlin

 var intent = Intent(this, [Your_activity]:class.java)
        intent.flags =  Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
        startActivity(intent)
0
On
val intent = Intent(this, MyActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)

add to Manifest android:launchMode="singleTask

0
On
 val intent = Intent(activity!!, MainActivity::class.java)
    startActivity(intent)
    activity!!.finishAffinity()