Android Navigation Component Activity Intent Flags
Solution 1:
I had to hack my way through the same problem. To solve this, the first thing you have to do is to create an action to navigate to the Activity, as you already had done.
For example:
<action
android:id="@+id/action_frag_to_myActivity"
app:destination="@id/myActivity"
app:popUpTo="@id/myActivity" />
Now, you can pass arguments to the Activity as intent extras, so you can take advantage of that to make the destination Activity do the "dirty work" and clear the back stack for you.
Say that you have this Activity tag inside your navigation graph:
<activity
android:id="@+id/myActivity"
android:name="com.dummy.MyActivity"
android:label="activity_my" />
You could add an argument in it and add a default value. For example:
<activityandroid:id="@+id/myActivity"android:name="com.dummy.MyActivity"android:label="activity_my"><argumentandroid:name="clearBackstack"app:argType="boolean"android:defaultValue="true" /></activity>
Then once you call findNavController().navigate(R.id.myActivity)
it'll pass an intent extra with the key "clearBackstack"
which you can read inside the Activity onCreate()
method. Something like the example below.
MyActivity.kt
privateval EXTRA_LOGOUT = "clearBackstack"overridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
if (intent.extras?.getBoolean(EXTRA_LOGOUT) == true) {
clearBackstack()
} else {
setContentView(R.layout.activity_my)
}
}
privatefunclearBackstack() {
startActivity(Intent(this, MyActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK or Intent.FLAG_ACTIVITY_NEW_TASK)
})
finish()
}
Keep in mind you can tinker around with the arguments and customize what you want to do on the destination Activity. You could also modify the value once you navigate to it. You can read more about it here in the docs.
Solution 2:
Referencing Fatih's answer
valextras= ActivityNavigator.Extras.Builder()
.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
.build()
findNavController().navigate(BlaBlaActivityDirections.actionFooBarToBlaBlaActivity(), extras)
Note You need to add these flags to the activity stack:
FLAG_ACTIVITY_CLEAR_TASK
: sets the activity as the root task. used withFLAG_ACTIVITY_NEW_TASK
: treats the activity as launcher activity.FLAG_ACTIVITY_REORDER_TO_FRONT
: brings the activity to front. Use this only if you have other activities on the stack and you don't want to clear it.
Solution 3:
You can control properties like singleTop from the action element, in the navigation definition.
The properties are available in the navigation editor as well.
Solution 4:
You can add an intent flag to navigator extras.
valextras= ActivityNavigator.Extras.Builder()
// Add your flags
.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT)
.build()
// Navigate with extras
findNavController().navigate(destination, extras)
You can check how the intent flags are handled in ActivityNavigator.navigate()
from the source code
Alternatively, you can use navigation options if the only flag you will set is FLAG_ACTIVITY_SINGLE_TOP
.
valnavOptions= NavOptions.Builder().setLaunchSingleTop(true).build()
findNavController().navigate(destination, navOptions)
You can check how nav options are checked in ActivityNavigator.navigate()
from the source code
Solution 5:
If you are moving from one activity to another.
That's how I solved the same question. I hope it helps.
firstIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
Post a Comment for "Android Navigation Component Activity Intent Flags"