6

I have an application that has a menu and depending on what button you press in the menu a new activity is opened. I want to have a back button on every screen that will bring you to the previous screen so I'm wondering how do I go about this?

Here is some code I have used that works:

backButton = (ImageButton) findViewById(R.id.back_button);
        backButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


                finish();

            }
        });

However its not good programming practice for me to put this code in all my activities. How do I go about creating some kind of stack that saves all the pages viewed and using that to return to the previous page?

I have to put a back button in my application so I cannot use the existing one in the ActionBar.

krsteeve
  • 1,794
  • 4
  • 19
  • 29
DMC
  • 1,184
  • 5
  • 21
  • 50

6 Answers6

10

just an Idea

Create a baseClass which extends Activity. In there declare

    @Override
    public void onClick(View v) {
         super.onBackPressed(); // or super.finish();
    }

In all activity extend this Base Class. And in every layout in the button put

   android:onClick="onClick"

And to make the xml design of button reusable create it in a separate xml. and add it using <include/>

stinepike
  • 54,068
  • 14
  • 92
  • 112
8

Have you tried using action bar in your activity ? use in every activity

ActionBar actionBar = getSupportActionBar();
if(actionBar != null){
    actionBar.setTitle(getResources().getString(R.string.app_name));
    actionBar.setDisplayHomeAsUpEnabled(true);
    actionBar.setIcon(R.drawable.app_icon);
}

and handle in

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}
Komal12
  • 3,340
  • 4
  • 16
  • 25
Rahul Patil
  • 2,707
  • 2
  • 21
  • 32
  • 3
    The actual icon did not change for me until I replaced actionBar.setIcon(R.drawable.app_icon); with actionBar.setHomeAsUpIndicator(R.drawable.app_icon); – Tamarisk Sep 01 '18 at 21:40
  • 1
    Answer is misleading, As @Tamarisk mentioned instead of `actionBar.setIcon(R.drawable.app_icon)` use `actionBar.setHomeAsUpIndicator(R.drawable.app_icon)` – Rafael Oct 01 '18 at 09:54
  • https://developer.android.com/reference/android/app/ActionBar.html#setIcon(int) The answer was given 5 years back. Check if you are using Toolbar as actionbar . or non support actionbar – Rahul Patil Oct 01 '18 at 11:43
5

I would suggest against having a custom back button. Android has a hardware back button. Pressing the haradware back button will navigate to the previous.

I don't think you need a custom back button. I don't think its a good programming practice to override the default behaviour.

You create a backbutton in your activity and implementing the functionality as you have done above. Still the user can use the hardware back button for the same functionality. So you would be providing the same functionality which is redundant.

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
1

There is a hardware back button on all android devices, and it does exactly what your lines of codes do, unless overridden to do something else.

You can refer to this answer as well.

Community
  • 1
  • 1
ASamsig
  • 446
  • 3
  • 16
0

I had a similar issue where I was using a back button "Cancel" and a home screen "homeActivity". I ended up solving it using this:

CancelButton = (Button) findViewById(R.id.cancel_button);
        CancelButton.setOnClickListener(new OnClickListener() {

             public void onClick(View view) {
                 Intent homeActivity = new Intent(getApplicationContext(), DeviceListActivity.class);

                 startActivity(homeActivity);
                 finish();  
            }
        });
0

In kotlin

 backButton!!.setOnClickListener(View.OnClickListener {
 onBackPressed()
  })
Niaj Mahmud
  • 399
  • 3
  • 10