1

I want to make a button that allow user to go back to the previous page when click. Just work like the physical back button on the android device. What should I add to the java file?

here is the code(xml):

<Button
        android:id="@+id/button00"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:layout_marginTop="5dp"
        android:layout_marginBottom="5dp"
        android:layout_marginLeft="10dp"
        android:text="@string/st_pp"
        android:textColor="#01646d"
        android:background="#fef200"/>

here is the code(java):

bn00.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent it1 = new Intent(getApplicationContext(), Main.class);
                startActivity(it1);
            }

        });
21.kaw
  • 583
  • 2
  • 7
  • 18

8 Answers8

11

To go back to previous activity use finish() method. But note that previous activity not contains finish() while you call current activity.

Code :

bn00.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {

                finish();
            }

        });

Using above code you can go to your previous activity. You can also call finish() in onKeyDown() method like..

@Override
public boolean onKeyDown(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
       finish();
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
1

What you want is to use finish() on your current Activity. It will remove current Activity from the stack, display previous one and therefore it will work as back button.

bn00.setOnClickListener(new OnClickListener(){
    @Override
    public void onClick(View arg0) {
        finish();
    }
});
Piotr Chojnacki
  • 6,837
  • 5
  • 34
  • 65
1

You can just call a finish() to the current activity, it should go back to the previous one.

For further info see the javadoc

Luiz Cavalcanti
  • 243
  • 1
  • 6
1

The finish(); method will end the current activity and show the previous.

But the onBackPressed(); method will press the back button. But the default implementation (without an Override) will call the finish(); method anyway.

string.Empty
  • 10,393
  • 4
  • 39
  • 67
0
bn00.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                //Intent it1 = new Intent(getApplicationContext(), Main.class);
                //startActivity(it1);

                finish(); //just add this
            }

        });
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
Changwei Yao
  • 13,051
  • 3
  • 25
  • 22
0

Android has a hardware back button which does the job. So i would recommend against have a button that does the same. When hardware back button is pressed the current activity in the back stack is popped, destroyed and the previous activity in the back stack takes focus.

You call finish() on button click as below

  bn00.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {
           finish();
        }

    });
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

Instead of "bn00" write "button00" in your code because you have set "button00" as your button id in your xml. And as said by other's too, call finish() inside onClick() method. Which will end your current activity and load your previous activity which has not finished yet.

buttonn00.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View arg0) {

            finish();
        }

    });
Shubham Arora
  • 167
  • 13
0

In Kotlin, You just need:

bn00.setOnClickListener { finish() }
Rumit Patel
  • 8,830
  • 18
  • 51
  • 70