21.kaw
21.kaw

Reputation: 593

Button to go back to the previous page

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);
            }

        });

Upvotes: 1

Views: 26607

Answers (8)

Silesio Cipriano
Silesio Cipriano

Reputation: 11

In Kotlin, You just need:

bn00.setOnClickListener { finish() }

Upvotes: 0

Hardik Joshi
Hardik Joshi

Reputation: 9507

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);
}

Upvotes: 11

Nicolas Tyler
Nicolas Tyler

Reputation: 10552

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.

Upvotes: 1

Shubham Arora
Shubham Arora

Reputation: 167

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();
        }

    });

Upvotes: 0

Changwei Yao
Changwei Yao

Reputation: 13101

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
            }

        });

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133570

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();
        }

    });

Upvotes: 0

Luiz Cavalcanti
Luiz Cavalcanti

Reputation: 243

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

For further info see the javadoc

Upvotes: 1

Piotr Chojnacki
Piotr Chojnacki

Reputation: 6867

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();
    }
});

Upvotes: 1

Related Questions