0

Just to clarify what I am asking: I am already closing the toast when call OnStop()/OnPause()/Edit Text OnClick(). Despite the title looks like, this isn't a duplicate of: How to stop displaying message from Toast when Application is closed? (sorry, I couldn't found I better title)

So I'm not asking how to close the toast, but how can I update the toast closed before finish the app.

Just to give an example, the alarm clock native of my nexus 5 (4.4.4) shows a notification when you delete an alarm setted. If when close/minimize the app, it close the toast, then close/minimize the app. I would know a code that allow me to do like that.

This is my sample code:

public class MainActivity extends Activity {
        private Toast toast;
        private Button buttonClick;
        private EditText editTextClick;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            buttonClick = (Button) findViewById(R.id.buttonClick);
            editTextClick = (EditText) findViewById(R.id.editTextClick);

            buttonClick.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    if (toast != null) {
                        toast.cancel();
                    }
                    toast = Toast.makeText(this, "This might close before the app stop", Toast.LENGTH_SHORT);
                    toast.show();
                }
            });


             editTextClick.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    toast = Toast.makeText(this, "This might close before the keyboard open", Toast.LENGTH_SHORT);
                    toast.show();
                    if (toast != null) {
                         toast.cancel();
                    }
                }
            });

        }

        @Override
        protected void onStop() {
            // TODO Auto-generated method stub
            if (toast != null) {
                toast.cancel();
            }

            super.onStop();
        }

        @Override
        protected void onPause() {
            // TODO Auto-generated method stub
            if (toast != null) {
                toast.cancel();      // This close toast when stop called 
            }
            super.onPause();
        }

    }

I am trying to close a the toast notification ever time the edit text is clicked. But it first open the keyboard (overlapping the toast and keyboard for a while) than close the toast.

The same happen when I close the app or alternate app. The toast disappear after overlapping the other layout.

I have already try to use a Thread.sleep(1000); (within a try-catch clause) and after if(toast != null) { toast.cancel(); }, but doesn't change anything.

What I could do in this sample code to make it closes the toast then do others things (like open the keyboard, close the app, show recent apps, etc).

4

1 に答える 1