Oct 15, 2011

Handle the BACK key by overriding onBackPressed()

For API level 5 (Android 2.1) or higher, onBackPressed() callback method is provided. It will be called when the activity has detected the user's press of the back key. The default implementation simply finishes the current activity, but you can override this to do whatever you want.

package com.BackButton;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.Toast;

public class AndroidBackButtonActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}

@Override
public void onBackPressed() {
// TODO Auto-generated method stub

Toast.makeText(AndroidBackButtonActivity.this,
"BACK key pressed",
Toast.LENGTH_LONG).show();

}


}



Related Post:
- Handle the BACK key by overriding onKeyDown() (For Android OS version before API level 5)

No comments:

Post a Comment

Infolinks In Text Ads