Creating custom back button in android

android, android-button

Solution

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

Problem

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.

Original source

Related problems