Ran into your question looking for an answer for a similar problem.
In my case, I have a single Activity app which is in Fragment B when the permission state is changed, and also reopens in Fragment B after the Activity is restarted. Like you, I want my Activity to start with Fragment A, which is also the start or default destination of my nav graph.
The way I solved my problem was that, I would detect whenever the Activity had been exited because of a REASON_PERMISSION_CHANGE
, then I nullify the savedInstanceState
inside my Activity's onCreate()
.
Here's some code:
// MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(
when (requiresPermissionChangeRestart()) {
true -> {
Log.e(TAG "Resetting Activity after permissions manual change")
null
}
false -> {
savedInstanceState
}
}
)
}
...
private fun requiresPermissionChangeRestart(): Boolean = (getSystemService(Context.ACTIVITY_SERVICE)
as ActivityManager).let { am ->
am.getHistoricalProcessExitReasons(null, 0, 0)
.find {
it.reason == ApplicationExitInfo.REASON_PERMISSION_CHANGE
}
.run {
when (this != null) {
true -> {
Log.w(TAG, "Permissions for package $packageName where changed by the user")
true
}
false -> false
}
}
}
}
It might be kind of a hack, but this little trick might help you arrive at a more fitting solution for your particular problem. As long as you get rid of the savedInstanceState
from the given Activity when it matters, you should be able to restart your Activity the way you would expect it to do so.