Dismiss activity in android
15,727
Solution 1
I have tested your code and modified little. Please check below
package test.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.btnOK);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
}
Solution 2
Try like this....
okBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
finish();// Closing Activity
}
});
Author by
indira
Updated on June 04, 2022Comments
-
indira almost 2 years
I am using the following code to show “Hello” message with ok button when the user clicks a button. In some applications this is working fine. ie while clicking the ok button, the activity is dismissed. But in one application, this is not getting dismissed after clicking the ok button. What to do? Please help.
public class MyClass extends Activity { private TextView labelTxt; private Button okBtn; @Override protected void onCreate(Bundle icicle) { super.onCreate(icicle); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.message); labelTxt = (TextView) findViewById(R.id.txt); labelTxt.setText("Hello"); okBtn = (Button) findViewById(R.id.okBtn); okBtn.setOnClickListener(okBtnClickListener); } private final OnClickListener okBtnClickListener = new OnClickListener() { public void onClick(View v) { finish(); } }; }