Pecurka
Pecurka

Reputation: 113

Handler Android Studio

I have a handler in android app and this works realy good

Down is the code for my handler

Runnable mStatusChecker = new Runnable() {
    @Override
    public void run() {
        try{
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            downloadusernamelist();
            isInternetWorking();
            syncDataSftp();
        }finally {
            mHandler.postDelayed(mStatusChecker, mInterval);
        }}};

Now this handler is on launcher activity and when i go to next activity and go back to first launcher activity the handler starts again...

How can i make that the handler not restarts every time i go back to first activity?

Upvotes: 0

Views: 375

Answers (1)

Efe Budak
Efe Budak

Reputation: 659

You can define a boolean such as;

private boolean firstTime = true;

Then you can decide on starting the handler according to firstTime boolean value.

if(firstTime){
   runHandler();
   firstTime = false;
}

You should also save firstTime value onSaveInstance and restore that value onCreate;

Upvotes: 1

Related Questions