Sudarshan Wadwale
Sudarshan Wadwale

Reputation: 74

How to remove call back when application goes to background?

I use runnable method they work properly when application goes background and app goes foreground they work but first time without background they cant work. work. My code is below.

 runnable = new Runnable() {
                        public void run() {
                            pd.dismiss();
                            Toast.makeText(context, "Login Successfully", Toast.LENGTH_SHORT).show();
                            Intent intent = new Intent(getApplicationContext(), WSSD.class);
                            toEdit.putString("LoginFlag", "true");
                            toEdit.commit();
                            intent.putExtra("AppMode", "online");
                            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                            intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                            startActivity(intent);
                            overridePendingTransition(R.anim.pull_in_right, R.anim.pull_out_left);

                            handler.postDelayed(this, 3000);
                        }
                    };

/////////////////////////

@Override
    protected void onResume() {
        handler.postDelayed(runnable, 5000);
        MyApplication.activityResumed();
        super.onResume();
    }

    @Override
    protected void onPause() {
        handler.removeCallbacks(runnable);
        MyApplication.activityPaused();
        super.onPause();
    }

Upvotes: 0

Views: 67

Answers (1)

abhishesh
abhishesh

Reputation: 3316

@Override
protected void onStop() {
    if(handler != null){
        handler.removeCallbacks(runnable);
    }
    super.onStop();
}

void removeCallbacks (Runnable r)

Remove any pending posts of Runnable r that are in the message queue.

Upvotes: 2

Related Questions