Search code examples
androidbackonbackpressed

how to copy back button action to another button


I have a button in which I want to have the same feature of the usual BACK button. But just calling the finish() is not doing the required.

I tried calling onBackPressed() but it needs to create a separate method. Any solution?

t1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            /*Intent i1=new Intent(getApplicationContext(), MainView.class);
            startActivity(i1); */
            //finish();

            this.onBackPressed() ;

        }


    });

Solution

  • In your code this.onBackPressed(); implies that your are calling a method of OnClickListener, which does not exists. Instead call it like this:

    Your_Activity_Name.this.onBackPressed();
    

    Where Your_Activity_Name is the name of your activity.