-1

I have made an application in that i have an activity contains a TextView and a button.Now this activity is used multiple times in the application so i want is that when i click the back button in that activity ,it should go to the previous activity.In short it should work as back button of device.My code is as below:

this activity is used in 4 activities ,

So i have tried as below:

Advance_help.java

package com.theappgeeks.kevingreenwealth;

import java.text.BreakIterator;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class AdvanceHelp extends Activity implements OnClickListener{
Button btn;
Calculator_1Activity cal1 =new Calculator_1Activity();
Calculator2 cal2 =new Calculator2();
Calculator_3Activity cal3 =new Calculator_3Activity();
Calculator_4Activity cal4 =new Calculator_4Activity();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_advance_help);
        btn=(Button)findViewById(R.id.button1);
        /*btn.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if(btn.)
            Intent i1=new Intent(AdvanceHelp.this,Calculator_1Activity.class);  
            startActivity(i1);

            }
        });*/
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_advance_help, menu);
        return true;
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        if(v.equals(cal1)){
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i1= new Intent(AdvanceHelp.this,Calculator_1Activity.class);
                    startActivity(i1);
                }

            });

        }
        else if(v.equals(cal2)){
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i1= new Intent(AdvanceHelp.this,Calculator2.class);
                    startActivity(i1);
                }

            });

        }

        else if(v.equals(cal3)){
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i1= new Intent(AdvanceHelp.this,Calculator2.class);
                    startActivity(i1);
                }

            });

        }

        else if(v.equals(cal4)){
            btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    Intent i1= new Intent(AdvanceHelp.this,Calculator2.class);
                    startActivity(i1);
                }

            });

        }
        else
        {
            Intent i = new Intent(AdvanceHelp.this,MainMenuActivity.class);
            startActivity(i);
        }
    }
}

Please help me as soon as possible..thank you in advance

jimmy cool
  • 147
  • 3
  • 5
  • 14

4 Answers4

1

Bad programming practice,

As you already implements OnClickListener

btn=(Button)findViewById(R.id.button1);
btn.setOnClickListener(this);

and remove

 btn.setOnClickListener(new OnClickListener() {

from

@Override
    public void onClick(View v) {

And for

i want is that when i click the back button in that activity ,it should go to the previous
activity.In short it should work as back button of device.My code is as below:

Just call Activity's finish() in button's click() on which you want..

user370305
  • 108,599
  • 23
  • 164
  • 151
0

Simply call the Method onBackPressed(). This Method is also called when you Push the Back Button.

Thommy
  • 5,070
  • 2
  • 28
  • 51
0

For backpressed and finishing the above answers are true but this is not an appropriate question you having one button and you want to call 4 activity that's not possible...!!!! because you are comparing it with id and that only matters when you have more than on button.....!!!!!

Ruchit Shah
  • 367
  • 1
  • 2
  • 10
0

in your onClick(View v) method call either super.OnBackPressed() to emulate the back button being pressed, or call finish() to end the current activity and go back to the previous activity that was running

AndroidNoob
  • 2,771
  • 2
  • 40
  • 53