title: Doubt with KeyEvent.KEYCODE_BACK in Android
body: <p>I have 3 activities. 
  1st activity has a 'go right' image button. (to move to 2nd activity)
  2nd activity has both 'go right' and 'go left' image buttons. (to go back to 1st activity or move to 3rd activity). 3rd activity has a 'go left' button to move to 2nd activity.
  And I have created events for back key press.</p>
  
  <p>Problem is,
  You navigate from 1st to 3rd, come back and then navigate to 3rd activity.
  1st activity---clicking go right----> 2nd activity---clicking go right-----> 3rd activity---clicking go left----> 2nd activity----clicking go left----> 1st activity----clicking go right----> 2nd activity---clicking go right-----> 3rd activity.</p>
  
  <p>Now if I press back key on the 3rd activity, it moves to 2nd. and on pressing back key here, it moves to 1st. But here if I press back key, it again goes to 1st activity. Only the 2nd back key press on 1st activity exits the application. An additional question is how do I make the Imagebutton visible only If I press there?</p>
  
  <p>Do I make any sense?</p>
  
  <p>FirstActivity.java</p>
  
  <pre><code>package com.stylingandroid.Animation;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  
  import android.view.KeyEvent;
  import android.view.Menu;
  import android.view.MenuInflater;
  import android.view.MenuItem;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ImageButton;
  
  public class FirstActivity extends Activity
  {
      @Override
      protected void onCreate( Bundle savedInstanceState )
      {
          super.onCreate( savedInstanceState );
          setContentView( R.layout.first );
          ((ImageButton)findViewById( R.id.NextButton )).setOnClickListener( new OnClickListener()
          {
              @Override
              public void onClick( View v )
              {
                  startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
                  overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
              }
          });
      }
      @Override
      public boolean onKeyUp( int keyCode, KeyEvent event )
      {
          if( keyCode == KeyEvent.KEYCODE_BACK )
          {
              finish();
              return true;
          }
          return super.onKeyUp( keyCode, event );
      }
  
      public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.options_menu, menu);
            return true;
          }
  
      public boolean onOptionsItemSelected(MenuItem item) 
      {
            switch (item.getItemId()) 
            {
            case R.id.next:
                startActivity( new Intent( FirstActivity.this, SecondActivity.class ) );
                overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
                return true;
  
            case R.id.exit:
                  finish();
  
            default:
                  return super.onOptionsItemSelected(item);
            }
      }
  
  }
  </code></pre>
  
  <p>SecondActivity.java</p>
  
  <pre><code>package com.stylingandroid.Animation;
  import android.app.Activity;
  import android.content.Intent;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.ImageButton;
  
  public class SecondActivity extends Activity
  {
      private ImageButton nextbutton;
      private ImageButton backbutton;
      @Override
      protected void onCreate( Bundle savedInstanceState )
      {
          super.onCreate( savedInstanceState );
          setContentView( R.layout.second );
          nextbutton = (ImageButton) findViewById(R.id.NextButton);
          backbutton = (ImageButton) findViewById(R.id.BackButton);
          nextbutton.setOnClickListener( new OnClickListener()
          {
              @Override
              public void onClick( View v )
              {
                  startActivity( new Intent( SecondActivity.this, ThirdActivity.class ) );
                  overridePendingTransition( R.anim.slide_in_left, R.anim.slide_out_left );
              }
          });
          backbutton.setOnClickListener( new OnClickListener()
          {
              @Override
              public void onClick( View v )
              {
                  finish();
                  startActivity( new Intent( SecondActivity.this, FirstActivity.class ) );
                  overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
              }
          });
      }
  
      @Override
      public boolean onKeyUp( int keyCode, KeyEvent event )
      {
          if( keyCode == KeyEvent.KEYCODE_BACK )
          {
              finish();
              overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
              return true;
          }
          return super.onKeyUp( keyCode, event );
      }
  }
  </code></pre>
  
  <p>ThirdActivity.java</p>
  
  <pre><code>package com.stylingandroid.Animation;
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.KeyEvent;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.ImageButton;
  
  public class ThirdActivity extends Activity
  {
      @Override
      protected void onCreate( Bundle savedInstanceState )
      {
          super.onCreate( savedInstanceState );
          setContentView( R.layout.third );
          ((ImageButton)findViewById( R.id.BackButton )).setOnClickListener( new OnClickListener()
          {
              @Override
              public void onClick( View v )
              {
                  goBack();
              }
          });
      }
  
      private void goBack()
      {
          finish();
          overridePendingTransition( R.anim.slide_in_right, R.anim.slide_out_right );
      }
  
      @Override
      public boolean onKeyUp( int keyCode, KeyEvent event )
      {
          if( keyCode == KeyEvent.KEYCODE_BACK )
          {
              goBack();
              return true;
          }
          return super.onKeyUp( keyCode, event );
      }
  }
  </code></pre>