0

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!

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);
    }
}
KGBird
  • 789
  • 10
  • 9
  • 1
    Just a guess, but maybe you need to separate the classes into different files. If you haven't done that already. – Philip Rego Dec 29 '15 at 01:48

5 Answers5

2
  1. do not use finish() because it will destroy your activity instead of paused. if you don't use finish() you can automatically use the back button and it will go back according to the stack.
  2. if for some reason you need to use finish(), you can implement the override back key button (just like the other answer stated) in every class and manually write code to open specific activity when the back button pressed.

for example:

 @Override
 public void onBackPressed()
 {
     super.onBackPressed();
     startActivity(new Intent(Activity2.this, MainActivity.class);
     finish()
 }
A. N
  • 153
  • 1
  • 15
1

Do not call finish() when you start activity2 and activity3. implement an override onBackPressed in the MainActvity class, then call finish in there see below.

@Override
public void onBackPressed() {
    super.onBackPressed();
    finish();
}
CodeDaily
  • 756
  • 4
  • 17
1

remove finish(); finish()means your want close current Activity

codingnow
  • 94
  • 4
0

I using only one activity with fragments. In this case you can use onBackPressed() in main Activity (only once in app)

 @Override
 public void onBackPressed()
 {
     super.onBackPressed();
 }

more info at How to implement onBackPressed() in Android Fragments?

Community
  • 1
  • 1
Milos Lulic
  • 627
  • 5
  • 22
  • This code is not useful, it's the same as if you don't implement the `onBackPressed` method in your activity. – hunyadym Dec 29 '15 at 02:28
  • It is just exemple, there is link for more information, or he can use to google it...best way to learn – Milos Lulic Dec 29 '15 at 02:47
0

My suggestion is you should finish the current activity after you start a new activity Then

@Override public void onBackPressed() { super.onBackPressed(); Intent toOpen = new Intent(CurrentActivity.this,ActivityToOpen.class); startActivity(toOpen); finish() }

this might help you.

CLIFFORD P Y
  • 16,974
  • 6
  • 30
  • 45