Android using back button
<p>i wanna use the back button on the mobile itself to make a dialog box appear to ask me whether am sure i wanna exit or not?</p>

<pre><code> public void onBackPressed() {
    // TODO Auto-generated method stub

    AlertDialog.Builder dialog=new AlertDialog.Builder(MainActivity.this);
    dialog.setTitle("Confirm");
    dialog.setMessage("Are you sure you want to exit?");
    return;
 }



@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    EditText text=(EditText)findViewById(R.id.Text);
    TextWatcher watch=new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                int after) {
            // TODO Auto-generated method stub

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            onBackPressed();
        }
    };
    text.addTextChangedListener(watch);
}
</code></pre>
