0

Sorry for my bad English.

I have two activity. called Act1 and Act2

Act2 will be started when a Button in Act1 is clicked (startActivity). My question is how to disabled back button in Act2 so, I cannot return to Act1.

Is it possible. Thank you.

How to overcome this problem :

Override onKeyDown Method, end Set finished();

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK)) {
        finish();
    }
    return super.onKeyDown(keyCode, event);
}
farissyariati
  • 365
  • 4
  • 9
  • 21

7 Answers7

1

It's been asked before, so you'll find plenty of good answers here:

Override back button to act like home button

Just override the back key and call finish() instead. That way it won't go back to the first activity, it'll just close.

Community
  • 1
  • 1
Michell Bak
  • 13,182
  • 11
  • 64
  • 121
  • I think it's better to set android:noHistory="true" or use the Intent.FLAG_ACTIVITY_NO_HISTORY – Dante Dec 22 '11 at 16:30
  • @Dante Depends on what you need to do, but I suppose that's one way to do it :) – Michell Bak Dec 22 '11 at 16:54
  • @user1052070 Looking at your history on Stack Overflow, I see that you haven't accepted any answers in the past. Please go ahead and mark the correct answers as accepted. That'll also help you in the future here. – Michell Bak Dec 22 '11 at 16:59
  • This would cause problems if there is an Activity3 and the back button should go to Activity3 from Activity2 (since user has browsed Act1, Act2, Act3, Act2), or if there are Fragments used in Activity2 and added to the backstack that the back button should recover. ----- Of course, you can add logic to the back button override to handle all this, but then things start to get messy. – Peter Ajtai Dec 22 '11 at 17:06
  • He made it very clear that he had two activities, so I really don't see the problem. – Michell Bak Dec 22 '11 at 17:07
1

After startActivity(i); call finish() to close previous activity. or use Intent.FLAG_ACTIVITY_NO_HISTORY

Yajushi
  • 1,175
  • 2
  • 9
  • 24
0

There are several answers. Some you can find here: Removing an activity from the history stack

You can also override the backbutton to do exactly what you want.

Community
  • 1
  • 1
Dante
  • 1,104
  • 1
  • 10
  • 15
0

Basically you need this,

Intent.FLAG_ACTIVITY_CLEAR_TASK

This clears your current activity stack, i.e.

if you have activity A and it starts activity B with this flag pressing the back button will not return you to Activity B because your stack now has only B.

This method is cleaner and requires less code than overriding the back button.

MahdeTo
  • 11,034
  • 2
  • 27
  • 28
0

Since it sounds like you want to make it so the back button doesn't go back to Activity 1, but you probably still need a functional back button in Acitity 2 (maybe to deal with a potential Activity 3 or to handle the Fragment Backstack), the simplest thing is to leave the back button functionality as is, and simply wipe Activity 1 from the backstack. Do this by calling finish() on Activity 1 as you switch to Activity 2:

// This is in Activity 1. It is how we switch to Activity 2:
Intent intent = new Intent(this, Activity2.class);      
startActivity(intent);
finish();
// And now Activity 1 cannot be navigated back to with the back button
// but the back button functionality remains intact.
Peter Ajtai
  • 56,972
  • 13
  • 121
  • 140
0

If you leave it like below to I presume it wont crash

@Override 

public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) {

} 
return super.onKeyDown(keyCode, event); 

}

Karthik
  • 4,943
  • 19
  • 53
  • 86
0

use this code in activiy 2...

public boolean onKeyDown(int keyCode, KeyEvent event) {

             switch(keyCode){
             case KeyEvent.KEYCODE_BACK:

             break;
             }
             return false;
             }
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58