57

I know we have back button in android to move us back on the previous form, but my team leader asked to put a back button functionality on button click

How can I do this?

Alex
  • 8,093
  • 6
  • 49
  • 79
Sourabh
  • 5,170
  • 9
  • 30
  • 41

7 Answers7

127

You should use finish() when the user clicks on the button in order to go to the previous activity.

Button backButton = (Button)this.findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    finish();
  }
});

Alternatively, if you really need to, you can try to trigger your own back key press:

this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN, KeyEvent.KEYCODE_BACK));
this.dispatchKeyEvent(new KeyEvent(KeyEvent.ACTION_UP, KeyEvent.KEYCODE_BACK));

Execute both of these.

Jyotman Singh
  • 10,792
  • 8
  • 39
  • 55
sahhhm
  • 5,325
  • 2
  • 27
  • 22
  • i can't use finish()... actually i have a form named as schedule dates form on that i want to use schedule and back button... schedule button will schedule dates and in case user wants to cancel scheduling dates he can cancel it by using the back button functionality on cancel button.... – Sourabh Jan 10 '11 at 18:43
  • 7
    In order to trigger the backbutton through the dispatchKeyEvent you need to trigger both the ACTION_DOWN followed by the ACTION_UP. This mimics a button click. Without having both, the back button will not trigger. – Ifrit Jul 18 '13 at 17:54
  • 3
    Does BackButton always finish ? I don't think so. What about a fragment operation ? – alicanbatur Dec 10 '13 at 12:08
  • @JaySoyer comment by JaySoyer is CORRECT for my case, to be able to simulate the back button pressed event, I have to dispatch both ACTION _DOWN and ACTION_UP event! – toantran Jan 22 '14 at 03:58
  • Actually, I tried all these methods: `finish()`, `onBackPressed()` and dispatching the key event, and none worked as I expected. The difference between all these methods and actual back click is that the back click calls directly the onResume() method on the previous activity, but the others call the the onCreate() method. I don't know how to simulate the exact behaviour... – hiddeneyes02 Mar 22 '18 at 10:06
37

If you need the exact functionality of the back button in your custom button, why not just call yourActivity.onBackPressed() that way if you override the functionality of the backbutton your custom button will behave the same.

ekawas
  • 6,594
  • 3
  • 27
  • 39
  • i can't use finish()... actually i have a form named as schedule dates form on that i want to use schedule and back button... schedule button will schedule dates and in case user wants to cancel scheduling dates he can cancel it by using the back button functionality on cancel button.... – Sourabh Jan 10 '11 at 18:44
  • 12
    not sure how that comment relates to my answer ... i am not asking you to call finish(), but to call onBackPressed() from your menu button click. – ekawas Jan 10 '11 at 20:43
10
public boolean onKeyDown(int keyCode, KeyEvent event) {
             if (keyCode == KeyEvent.KEYCODE_BACK) {
                     // your code here
                     return false;
             }
         return super.onKeyDown(keyCode, event);
     }
Sourabh
  • 5,170
  • 9
  • 30
  • 41
8

layout.xml

<Button
    android:id="@+id/buttonBack"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="finishActivity"
    android:text="Back" />

Activity.java

public void finishActivity(View v){
    finish();
}

Related:

Community
  • 1
  • 1
Brunofs
  • 81
  • 1
  • 1
6

If you are inside the fragment then you write the following line of code inside your on click listener, getActivity().onBackPressed(); this works perfectly for me.

Atul Chaudhary
  • 1,491
  • 2
  • 15
  • 23
5

With this code i solved my problem.For back button paste these two line code.Hope this will help you.

Only paste this code on button click

super.onBackPressed();

Example:-

Button backButton = (Button)this.findViewById(R.id.back);
backButton.setOnClickListener(new OnClickListener() {
  @Override
  public void onClick(View v) {
    super.onBackPressed();
  }
});
GreenROBO
  • 4,725
  • 4
  • 23
  • 43
1

Well the answer from @sahhhm didn't work for me, what i needed was to trigger the backKey from a custom button so what I did was I simply called,

backAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyRides.super.onBackPressed();

            }
});

and it work like charm. Hope it will help others too.

Ussaid Iqbal
  • 786
  • 1
  • 7
  • 16