Search code examples
androidandroid-studiokotlinnavigationandroid-actionbar

Up Navigation in android


When I'm pressing the back button on phone or the support action bar, it's exiting the application. How can I fix it to return to the main activity In login code:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_login)
    
    supportActionBar?.setDisplayShowHomeEnabled(true) 
.... }
override fun onOptionsItemSelected(item: MenuItem): Boolean {
    when (item.itemId) {
        android.R.id.home -> {
            NavUtils.navigateUpFromSameTask(this)
            return true
        }
    }
    return super.onOptionsItemSelected(item)
}

And in the manifest file:

<activity
        android:name=".LoginActivity"
        android:label="Login"
        android:parentActivityName=".MainActivity">
    <meta-data
        android:name="android.support.PARENT_ACTIVITY"
        android:value="com.example.food_bank.MainActivity"/>
</activity>

Solution

  • So I removed "finish()" from all the intent codes and it worked by naming the parent activity in the manifest file