In my android app i am launching an activity using Intent. this activity uses a different layout which has a close ImageButton. I want to set the onClick event to close this activity and go back to the previous activity. i am using the finish() method to achieve this but nothing happens when i click on the close Image Button.
Following is the Code i am using:
To Launch the Activity:
public void stOn(){
Button sb = (Button)findViewById(R.id.sb);
sb.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
Intent scIntent = new Intent(MainActivity.this, St.class);
MainActivity.this.startActivity(scIntent);
}
});
}
And to Destroy the Activity:
public void stOff(){
ImageButton pw = (ImageButton)findViewById(R.id.pw);
pw.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
finish(); // Finish Current Activity
}
});
}
What am i doing wrong ?
As Kurosawa Hiroyuki say in comment your function
stOff()is maybe not called.That’s maybe something you already done but, more generally, in Android, we use to declare all the interface elements (
Button,ImageButton,EditText, …) as fields of the activity and applyingfindViewById()andsetOnClickListener(...)functions inside theonCreate(Bundle savedInstanceState)function.