Avoid Alert Dialog getting closed when back button pressed? - android

First thing I want to say, this was done by seeing a tutorial. Here is the Custom Alert Dialog activity part I am calling from a broadcast receiver. The only problem is the back button click. Once the Alert dialog activity got started, when I press the back button it is getting closed.
public class AlertDialogActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setFinishOnTouchOutside(false);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL);
setContentView(R.layout.activity_inmsgdialog);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
}
I have tried onBackPressed and I'm able to see the toast message but the activity is getting closed.

See here:
#Override
public void onBackPressed()
{
super.onBackPressed(); //Remove this line
Toast.makeText(getApplicationContext(), "Back Pressed", Toast.LENGTH_SHORT).show();
}
Do not call super.onBackPressed(); code if you want to disable back button for activity. So remove this line. Hope it helps.

You can use the below option to handle back button press
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
//your code
return true;
} else {
}
}

Don't propagate the event and you should be good.
#Override
public void onBackPressed()
{
//don't call super
}

Related

Make screen not clickable until a button is clicked

I'm working on a kotlin app, My activity is in fullscreen and I want to make a button in which the screen can't be exit or back or anything unless the button is clicked. Like the one in the video player lock type.
My first idea was to make boolean isLocked and after clicking button change this value to the opposite and override all functions/events etc which You would like to lock and if isLocked is true just don't execute them.
Here is a simple code where I locked back button (in java but it can be easily changed to Kotlin)
public class MainActivity extends AppCompatActivity
{
boolean isLocked = false;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
isLocked ^= true;
System.out.println(isLocked);
}
});
}
#Override
public void onBackPressed()
{
if (!isLocked)
{
super.onBackPressed();
}
}
}
I think that in a similar way You can lock every event.
You can also make something like this:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if (!isLocked)
{
return super.onKeyDown(keyCode, event);
}
else
{
return true;
}
}
This will cancel every event like clicking back button or changing volume

Back press event of device to dismiss a window

I have created a window and i am showing it on screen through Broadcast receiver.But the problem is that it appears on the screen and i want to dismiss it once the back button is pressed.I am unable to get the event of button press on this view.My code for back press looks as follows-
view.setOnKeyListener(new View.OnKeyListener() {
#Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KEYCODE_BACK) {
Log.d("LOG", "back button is pressed");
}
return true;
}
});
But Nothing is happening.I tried to do the same through DISPATCHKEY but it was also of no use.Please help me what i am not figuring out.Wouldn't this work on the view.?
Maintain global reference for Window and override onBackPressed()
Try this:
#Override
public void onBackPressed() {
if (view != null && view.isShowing()) {
view.dismiss();
} else {
super.onBackPressed();
}
}

android get key press programmatically

I want to know which key has been pressed in android keyboard. For example, if a is pressed {a}, I want to show value {a} in screen with toast ?
i want using broadcastreciever or background service
Try using dispatchKeyEvent(KeyEvent event) in your Activity:
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
public class MainActivity extends Activity {
KeyEvent event;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dispatchKeyEvent(event);
}
#Override
public boolean dispatchKeyEvent(KeyEvent event) {
Log.i("key pressed", String.valueOf(event.getKeyCode()));
return super.dispatchKeyEvent(event);
}
}
When I run this code, the keyboard app crashes. A dialog with message posted below is displayed.
Unfortunately,keyboard has stopped

Android Back Button Pressed Check

So I'm currently developing a turn based game app. And it displays toasts from a view on who's turn it is after each turn is taken. What's weird though, is if I hit the back button it will go back to the main menu(previous activity) but the toasts will still linger till they time out. Also if I hit back twice and it goes to the home screen, the toasts will still show until they finish. I want to implement a check or some way to cancel those toasts when the back button is pressed. I have to do this in my view too, my view contains all the toasts and all the code to the game, the gameActivity only has onCreate to create the view for the game. Any ideas?
In your Activity you have override method:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// do something
yourView.notifyBackPressed();
return false;
}
return super.onKeyDown(keyCode, event);
}
And in your View you have to implement method, for example notifyBackPressed().
Try This...
Try to use Toast.LENGTH_SHORT or
You should set duration (milliseconds) for custom Toast like this.
Custom toast:
LayoutInflater inflater = getActivity().getLayoutInflater();
View layoutToast = inflater.inflate(R.layout.custom_toast_layout,
(ViewGroup) getActivity().findViewById(R.id.toastcustom));
((TextView) layoutToast.findViewById(R.id.texttoast)).setText("I'm custom toast");
final Toast myToast = new Toast(
(EmployerNominationView) getActivity());
myToast.setView(layoutToast);
myToast.setDuration(300);
myToast.setGravity(Gravity.BOTTOM, 0, 45);
myToast.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
myToast.cancel();
}
}, 1000);
Generate Toast
Toast toast;
toast = Toast.makeText(getBaseContext(), "Messages", Toast.LENGTH_LONG);
toast.show();
Cancel the Toast you may use onKeyDown() or onBackPressed() .
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
toast.cancel();
return true;
}
return super.onKeyDown(keyCode, event);
}
//Try with making Toast Global and cancel the toast on Back Pr
Toast mToast = new Toast(ApplicationContext);
public void onBackPressed(){
mToast.cancel();
}
I found this and will hellp you
Android AppMsg (Crouton) Library
Implementation of in-layout notifications. Based on Toast notifications.
I try the below method to detect the back button pressed on the action bar in activity by the first method and the second one is used to detecting the mobile hardware button back or kill activity button.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
setResult(RESULT_CANCELED);
super.onBackPressed();
}

Override Dialog's OnBackPressed

How can I specifically override just the back button while within a dialog to finish the entire activity and not just the dialog.
Using setOnCancelListener and setOnDismissListener do not work because there are other times that I simply close the dialog without closing the whole activity behind it.
Edit
Thanks Shubayu that may work!
I was also able to access just the back button in a dialog through this function.
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (event.getKeyCode() == KeyEvent.KEYCODE_BACK){
finish();
}
return false;
}
});
Override
public void onBackPressed ()
of the activity and put in the way you want the behavior in it. Also set a boolean from your dialog which you use inside onBackPressed() of the Activity. if the boolean is true, run the disabling part of the onBackPressed() code else don't.
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK){
// your logic goes here
return true;
}
return super.onKeyDown(keyCode, event);
}
use the above code::
You can use : dialog.setOnCancelListener(.....)
first set dialog.setCancelable(true);
than you can place below code :
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
#Override
public void onCancel(DialogInterface dialog)
{
// add code backpress
}
});

Categories

Resources