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.

1
  • 1
    you have already done good programming practice. just like default behaviour. Commented Apr 19, 2013 at 12:07

6 Answers 6

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/>

Sign up to request clarification or add additional context in comments.

1 Comment

Wow! as i wanted thnx
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);
    }
}

3 Comments

The actual icon did not change for me until I replaced actionBar.setIcon(R.drawable.app_icon); with actionBar.setHomeAsUpIndicator(R.drawable.app_icon);
Answer is misleading, As @Tamarisk mentioned instead of actionBar.setIcon(R.drawable.app_icon) use actionBar.setHomeAsUpIndicator(R.drawable.app_icon)
developer.android.com/reference/android/app/… The answer was given 5 years back. Check if you are using Toolbar as actionbar . or non support actionbar
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.

2 Comments

this answer didn't age well :|
things have changed a lot since the question was answered :)
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.

Comments

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

Comments

0

In kotlin

 backButton!!.setOnClickListener(View.OnClickListener {
 onBackPressed()
  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.