Skip to content

Instantly share code, notes, and snippets.

@gbzarelli
Last active July 26, 2018 18:24
Show Gist options
  • Save gbzarelli/04260412f0b2ab1841c5dd1e6f5e1728 to your computer and use it in GitHub Desktop.
Save gbzarelli/04260412f0b2ab1841c5dd1e6f5e1728 to your computer and use it in GitHub Desktop.
Providing Up navigation from Notification
// https://developer.android.com/training/implementing-navigation/ancestral
override fun onSupportNavigateUp(): Boolean {
navigationUp(false)
return true
}
override fun onBackPressed() {
navigationUp(true)
}
private fun navigationUp(onBackPressed: Boolean) {
val upIntent: Intent? = NavUtils.getParentActivityIntent(this)
when {
upIntent == null -> throw IllegalStateException("No Parent Activity Intent")
NavUtils.shouldUpRecreateTask(this, upIntent) || intent.action == ACTION_NOTIFICATION -> {
TaskStackBuilder.create(this)
.addNextIntentWithParentStack(upIntent)
.startActivities()
}
else -> {
if (onBackPressed) {
super.onBackPressed()
} else {
NavUtils.navigateUpFromSameTask(this)
}
}
}
}
<application ... >
...
<!-- The main/home activity (it has no parent activity) -->
<activity
android:name="com.example.myfirstapp.MainActivity" ...>
...
</activity>
<!-- A child of the main activity -->
<activity
android:name="com.example.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment