Android Overriding onBackPressed()
Yes. Only override it in that one Activity
with
@Override
public void onBackPressed()
{
// code here to show dialog
super.onBackPressed(); // optional depending on your needs
}
don't put this code in any other Activity
override onBackPressed from interface
You shouldn't represent behavior by interface method, you should represent it by whole interface.
public interface OnBackPressedListener {
void onBackPressedEvent()
}
And then, you should attach this behavior to the activity/fragment/view root.
For example:
public class MyActivity extends Activity implements OnBackPressedListener {
// Initialize your activity's fields
private boolean isTaskRoot() {
// implement your task root method
}
@Override
public void onBackPressed() { // This method is provided by activity "from the box".
onBackPressedEvent();
}
@Override
public void onBackPressedEvent() {
if(isTaskRoot()) {
final Dialog dialog = new Dialog(this);
dialog.setContentView( R.layout.are_you_sure );
dialog.getWindow().setBackgroundDrawable( new ColorDrawable( Color.TRANSPARENT ) );
if (!this.isFinishing()) {
dialog.show();
}
}
}
}
Note that your onBackPressedListener
interface should NOT depend on any other methods, behaviors. Its main purpose is to provide an ability to mark the class as the class, which can react to onBackPressed event.
One of the possible usages is to inherit your fragments from onBackPressedListener
and then call their onBackPressedEvent
methods when parent Activity is notified that back button was pressed.
@Override
public void onBackPressed() {
// ...
if (currFragment instanceof onBackPressedListener) {
currFragment.onBackPressedEvent();
}
// ...
}
UPD: If you want to provide ready-to-use behavior for all classes inheriting onBackPressedListener
you can use default
keyword to create default interface method.
public interface onBackPressedListener() {
boolean isTaskRoot()
default void onBackPressedEvent() {
// Your implementation.
// It should not contain calls of methods, which are not presented by this interface.
if (isTaskRoot()) // ...
}
}
OR you can create BaseActivity
class, in which you will override onBackPressedEvent
from interface
public class BaseActivity extends Activity implements OnBackPressedListener {
@Override
public void onBackPressed() {
...
}
}
And then you inherit your activities from it
public class MyActivity extends BaseActivity { ... }
Android - How To Override the Back button so it doesn't Finish() my Activity?
Remove your key listener or return true
when you have KEY_BACK
.
You just need the following to catch the back key (Make sure not to call super in onBackPressed()
).
Also, if you plan on having a service run in the background, make sure to look at startForeground()
and make sure to have an ongoing notification or else Android will kill your service if it needs to free memory.
@Override
public void onBackPressed() {
Log.d("CDA", "onBackPressed Called");
Intent setIntent = new Intent(Intent.ACTION_MAIN);
setIntent.addCategory(Intent.CATEGORY_HOME);
setIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(setIntent);
}
Android onBackPressed for all activities
Create a single activity with onBackPressed
overrided:
public class OnBackPressedActivity extends Activity {
@Override
public void onBackPressed() {
// do stuff
}
}
And then in your code use not extends Activity
, but rather extends OnBackPressedActivity
Android - How to override onBackPressed method but still use default animation?
At the end of that method you have to call "super.onBackPressed()" to execute the normal behavior.
Override onBackPressed() method. Back button has to be clicked twice to quit the Activity
When you do this:
startActivity(new Intent(this, Menu.class));
This starts a new instance of Menu
. You want to return to the existing instance of Menu
. To do that, you need this:
Intent intent = new Intent(this, Menu.class));
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
This tells Android that you want to go back to the existing instance of Menu
.
Overriding onBackPressed in other Classes
If you want, for example, show a Toast
every time user presses a back button and on every screen, you have two options:
- Implement
onBackPressed
method in every activity and call some utility class to do that, for exampleToasts.showMessage()
. - You can inherit every activity from your
BaseActivity
where you can overrideonBackPressed
method and show toast there.
Related Topics
How to Get the List of Running Applications
Deleting Android Sms Programmatically
Android Timepickerdialog Set Max Time
How to Add Java.Awt.Image Package in Android
How to Downsample Images Correctly
Android Startcamera Gives Me Null Intent and ... Does It Destroy My Global Variable
How to Set Up Android Emulator Proxy Settings
How to Save a Bitmap on Internal Storage
How to Convert Coordinates of the Image View to the Coordinates of the Bitmap
No Internet on Android Emulator - Why and How to Fix
Android - Zoom In/Out Relativelayout with Spread/Pinch
How to Find Android Source Code Online
Gcm Push Notification Works After App Force Stop
How to Change Color and Font on Listview
Can't Get Write_Settings Permission
How to Upload an Image File in Retrofit 2
Change the Color of a Checked Menu Item in a Navigation Drawer
Build and Install Unsigned APK on Device Without the Development Server