Android stream still works after pressing return button

I want to stop the stream when I click the back button.
I use Handler.

+5
source share
6 answers

You need to use the function Handler removeCallbacks().

Code example:

@Override
public boolean onBackPress(int keyCode, KeyEvent event)  {
    if (keyCode == KeyEvent.KEYCODE_BACK) {
        handler.removeCallbacks(yourRunnable);
        return true;
    }

    return super.onKeyDown(keyCode, event);
}
+2
source

You cannot use methods stopor destroy. In the method onStopyou must use this .

+2
source

. onPause .

+1

"", onBackPressed() . , . , .

@Override
public void onBackPressed() {
    super.onBackPressed();
             thread.stop();
}
0

, .

Button back = (Button)findViewById(R.id.back);
back.setText("Back");
back.setOnClickListener(this);

private Handler handler = new Handler() 
{
@Override
    public void handleMessage(Message msg) 
    {
        super.handleMessage(msg);
        if(msg.obj.toString().contentEquals("hello"))
        {
            // do whatever u what to perform after thread stop....
        }
        removeDialog(0);
        }
 };

@Override
public void onClick(View v) 
{
    if(v==back)
    {   
        showDialog(0);
        t=new Thread()
        {
            public void run()
            {
                Message toMain = handler.obtainMessage();
                toMain.obj = "hello";
                handler.sendMessage(toMain); 
            }
        };          
        t.start();
    }
}

@Override
protected Dialog onCreateDialog(int id) 
{
             switch (id) 
        {
                    case 0: 
            {
                          ProgressDialog dialog = new ProgressDialog(this);
                          dialog.setMessage("Connecting . . . . . .");
                          dialog.setIndeterminate(true);
                          dialog.setCancelable(true);
                          return dialog;
                    } 
              }
              return null;
}

. , .

0

, handler.removeCallbacks(runnable); . :)

0

All Articles