Backbutton event issue, Help Me


I want to manage back button Activity with noHistory.
I add Two different Layout XML add With condition like this :

if(a == 0){attch onexml} if(a == 1){attch secondxml}

i attach two different activity_xml in single Activity using if condition . my App flow is

MainActivity -> (if(a == 0)onexml) if(a == 1)secondxml) 

Start app with MainActicity then click on button goto WorkActivity but first view onexml . again click button of onexml going to secondxml. when i press backbutton of toolbar , it going to MainActivity but i want to goto first onexml and then press back for MainActivity.

and my code for ActionBar is :

cat_toolbar.setNavigationOnClickListener {


Why not using fragment or custom view?


Why not just use a separate activity? It’s generally cleaner and less complex. If you don’t want the second activity on the back stack you can use Intent.FLAG_ACTIVITY_NO_HISTORY.

That said, if you really want to handle the back button this way, it’s not hard: Just override Activity.onBackPressed(). If you really want to go back, just call super(); if you don’t want to go back and want to handle it yourself, do whatever you want but don’t call super().