Rodrigofantino Rodrigofantino -4 years ago 184
Android Question

How to finish a handler before finishing an application?

Here is the thing I have a handler that shows a message on screen when I touch an area where the color of the background is white.

else if (ct.closeMatch (Color.WHITE, touchColor, tolerance))

{ Random r = new Random();
int txt= r.nextInt(6-0) + 0;
if(txt==0){ variables.pointtxt = "Nothing interesting"; }
else if (txt==1){ variables.pointtxt = "There´s nothing there"; }
else if (txt==2){ variables.pointtxt = "I can´t do nothing with that"; }
else if (txt==3){ variables.pointtxt = "Wait... nop nothing"; }
else if (txt==4){ variables.pointtxt = "Nothing"; }
else if (txt==5){ variables.pointtxt = "More nothing"; }
LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView,
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView text = (TextView) popupView.findViewById(R.id.popuptxt);
text.setText(variables.pointtxt);

popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 250) ;




new Handler().postDelayed(new Runnable()
{
public void run()
{ if (popupWindow.isShowing()== true)

popupWindow.dismiss();

}
}, 1000);




}


But if I run a new intent in les than 1000 miliseconds it crashes, I´m pretty sure because it couldn finish the handler cue.

Is it any way to tell the handler that if the aplication is closing then run popupWindow.dismiss(); ?

or inted is there a way to tell when I call ( when touching something red )

if (ct.closeMatch (Color.RED, touchColor, tolerance)) {


Intent game = new Intent(lvl2_1_0.this, lvl2_1_1.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

startActivity(game);



}


To finish all handler cues?

I´v been searching and I can´t find a way to force finish all the handlers in an aplication before finishing it.

I know I´m pushing it a little maybe but in some cases when I test the game it crashes, of course maybe in normal playing it won´t happen because it´s unprobable to press the button to pass the screen in les than 1000 milliseconds after apearing the popup But it bothers me to have that error on my hands.

Thanks to your answers in other threads I´v learned the most about programing for android so THANK YOU !

This is the code Fixed Thanks to sam ! :P

public class lvl2_1_0 extends Activity implements View.OnTouchListener {
private PopupWindow popupWindow;
private Handler mHandler = new Handler();
private Runnable dismissPopup = new Runnable() {
public void run() {
if (popupWindow.isShowing())
popupWindow.dismiss();
}
};
@Override
public void onCreate(Bundle savedInstanceState) {


Then

call the popup. In my case:

LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

View popupView = layoutInflater.inflate(R.layout.popup, null);
popupWindow = new PopupWindow(
popupView,
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT);
TextView text = (TextView) popupView.findViewById(R.id.popuptxt);
text.setText(variables.pointtxt);

popupWindow.showAtLocation(popupView, Gravity.CENTER, 0, 250) ;



mHandler.postDelayed(dismissPopup, 3000);


Finally close the aplication in without Force close

Intent game = new Intent(lvl2_1_0.this, lvl2_1_1.class);
game.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
game.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
mHandler.removeCallbacks(dismissPopup);
startActivity(game);

Answer Source

But if I run a new intent in les than 1000 miliseconds it crashes, I´m pretty sure because it couldn finish the handler cue.

Handler#removeCallbacks(Runnable) will cancel the passed Runnable f it is waiting in the queue.

Create two field variables to save a reference to your Handler and Runnable:

public class Example extends Activity {
    private Handler mHandler = new Handler();
    private Runnable dismissPopup = new Runnable() {
        public void run() {
            if (popupWindow.isShowing())
                popupWindow.dismiss();
        }
    }

Now where you previously called new Handler().postDelayed(new R... use:

mHandler.postDelayed(dismissPopup, 1000);

Finally just before you launch the new Intent call:

...
mHandler.removeCallbacks(mRunnable);`
startActivity(game);  

(You might want to call removeCallbacks() in onPause() as well.)

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download