How to run back button action with any other button on Android?

How can I perform the same back button action, but do it with any other interface button? for example, let's say I have an activity and from there I enter another activity that stays in the background, at the time of pressing the back button it returns me to the previous activity, but I want to do that without pressing the back button, I want that to be done by a button which I did in the application.

 4
Author: jasilva, 2016-05-17

6 answers

In case you want to perform the action on an activity, you already have your onbackpressed function present in your code. Now it's time to move it to another button. So we do the following:

 Button MyOnBackButton = (Button) mMainView.findViewById(R.id.myid);
    MyOnBackButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            onBackPressed();
            //Añade más funcionalidades
        }
    });

In case you want to add some extra functionality to the method, you should do the following:

@Override the onbackpressed () method in your Activity as follows:

@Override
public void onBackPressed()
{
     // Añade más funciones si fuese necesario
     super.onBackPressed();  // Invoca al método
}

Finally, in case you want to perform this action from a Fragment you'll need to instantiate your context (activity) and call the method from there. Being something like this:

mActivity.onBackPressed();

That's it! Copy this code to your app and everything should work properly.

Greetings!

 7
Author: Francisco Durdin Garcia, 2016-05-18 07:28:58

Calls the method of onBackPressed(); :)

 4
Author: cheko506, 2016-05-17 23:05:16

For example if you want a button in the options bar to be to go back.

Look at: Back button in activity title

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    // Respond to the action bar's Up/Home button
    case android.R.id.home:
        onBackPressed();
        return true;
    }
    return super.onOptionsItemSelected(item);
}
 1
Author: Webserveis, 2017-04-13 13:00:52

When clicking any button, calls the method onBackPressed() for example using the method onkeydown () you detect the key click and execute onBackPressed():

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
      Log.i("Ejemplo", "Se dio clic en la tecla: " + keyCode + " y se ejecutara onBackPressed()");

      onBackPressed();

    return super.onKeyDown(keyCode, event);
}
 1
Author: Jorgesys, 2016-05-17 23:00:54

You must first create the constructor as follows:

protected void onCreate(Bundle savedInstanceState) {

}

Later inside the constructor, you must put the following code:

Button anteriorrrr = (Button) findViewById(R.id.button6);
anteriorrrr.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        onBackPressed();
    }
});

The button called anteriorrrr is only declared in this place, as is in the code, so there is no need to do it again.

 0
Author: Sergio, 2017-05-15 15:16:37

From a fragment:

GetActivity ().onBackPressed ();

 -1
Author: MarioInk, 2018-07-05 22:12:43