@Override public void onBackPressed() { // Perform your desired action here super.onBackPressed(); }
@Override public void onBackPressed() { Intent intent = new Intent(ListActivity.this, MainActivity.class); startActivity(intent); finish(); }In this example, we are overriding the onBackPressed() method in a ListActivity to start a new activity when the back button is pressed. It will launch the MainActivity and finish the current activity. The package library for android.app.ListActivity is "android.app".