Question

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?

Was it helpful?

Solution

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().

OTHER TIPS

Try This...

  1. Try to use Toast.LENGTH_SHORT or
  2. 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);
    
  1. Generate Toast

    Toast toast;
    toast = Toast.makeText(getBaseContext(), "Messages", Toast.LENGTH_LONG);
    toast.show();
    
  2. 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();
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top