title: How to Get Android Back Button to Work
body: <p>I'm new to Android programming and I'm trying to get the Back button to work at all.  Everything I've read says that when the current activity starts another activity the new activity is pushed on top of the Back Stack and when the user presses the Back button the current activity is popped from the top of the stack and the previous activity resumes.  Most of the UI in my app is done programmatically.  Here's some simple code for three activities.  The general idea is that when the MainActivity appears, you press the button and Activity 2 is started.  Then in Activity 2, you press the button and Activity 3 is started.  At this point, if I press the system Back button on my device, my app exits.  Why doesn't the UI simply pop back to Activity 2 and if I press the system Back button again, the UI pop back to the Main Activity.  I'm sure there's something fairly basic I'm missing but I'd appreciate any help anyone could provide.  Thanks!</p>
  
  <pre><code>public class MainActivity extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          LinearLayout layout = new LinearLayout(this);
          layout.setOrientation(LinearLayout.VERTICAL);
          setContentView(layout);
  
          LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          layoutParams.gravity = Gravity.CENTER;
  
          TextView textView = new TextView(this);
          textView.setText("Main Activity");
          textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22f);
          textView.setLayoutParams(layoutParams);
          layout.addView(textView);
  
          Button button = new Button(this);
          button.setText("Start Activity 2");
          button.setLayoutParams(layoutParams);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Intent intent = new Intent(MainActivity.this, Activity2.class);
                  startActivity(intent);
                  finish();
              }
          });
          layout.addView(button);
      }
  }
  
  
  public class Activity2 extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          LinearLayout layout = new LinearLayout(this);
          layout.setOrientation(LinearLayout.VERTICAL);
          setContentView(layout);
  
          LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          layoutParams.gravity = Gravity.CENTER;
  
          TextView textView = new TextView(this);
          textView.setText("Activity 2");
          textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22f);
          textView.setLayoutParams(layoutParams);
          layout.addView(textView);
  
          Button button = new Button(this);
          button.setText("Start Activity 3");
          button.setLayoutParams(layoutParams);
          button.setOnClickListener(new View.OnClickListener() {
              public void onClick(View v) {
                  Intent intent = new Intent(Activity2.this, Activity3.class);
                  startActivity(intent);
                  finish();
              }
          });
          layout.addView(button);
      }
  }
  
  public class Activity3 extends Activity {
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
  
          LinearLayout layout = new LinearLayout(this);
          layout.setOrientation(LinearLayout.VERTICAL);
          setContentView(layout);
  
          LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
          layoutParams.gravity = Gravity.CENTER;
  
          TextView textView = new TextView(this);
          textView.setText("Activity 3");
          textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 22f);
          textView.setLayoutParams(layoutParams);
          layout.addView(textView);
      }
  }
  </code></pre>