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>
3
Answers
So I removed "finish()" from all the intent codes and it worked by naming the parent activity in the manifest file
what works for me:
MainActivity:
LoginActivity:
Manifest:
I guess switching from two times
supportActionBar?.setDisplayShowHomeEnabled(true)
tosupportActionBar?.setDisplayHomeAsUpEnabled(true)
has something to do with itChange your
onOptionsItemSelected
like thisand override
onBackPressed
method on your activity like this