Domanda

When I rotate the screen, the timer is destroyed and not re-created. For some reason, during the activity life-cycle, the timer does not resume to its state after the screen rotates.

@Override
public void onStart(){
    super.onStart();
    Log.d(CLASS_NAME, "onStart");

    if (timerRunning) {

        Log.d(CLASS_NAME,"This should give a new handler and UPDATE TIMER");

        handler = new Handler();
        updateTimer = new UpdateTimer();
        handler.postDelayed(updateTimer, UPDATE_EVERY);
    }
}

What I think the problem might be is that when the activity is destroyed when the screen rotates, the timerRunning becomes false, so it never reaches the if statement in onStart().

@Override
public void onPause(){
    super.onPause();
    Log.d(CLASS_NAME, "onPause");
}
@Override
public void onResume(){
    super.onResume();
    Log.d(CLASS_NAME, "onResume");

    enableButtons();

    Log.d(CLASS_NAME, "re-setting time display");
    setTimeDisplay();
}
@Override
public void onStop(){
    super.onStop();
    Log.d(CLASS_NAME, "onStop");

    if (timerRunning){

        Log.d(CLASS_NAME, "HERE ON STOP SHOULD REMOVE CALLBACKS");

        handler.removeCallbacks(updateTimer);
        updateTimer = null;
        handler = null;
    }
}

@Override
public void onDestroy(){
    super.onDestroy();
    Log.d(CLASS_NAME, "onDestroy");
}
@Override
public void onRestart(){
    super.onDestroy();
    Log.d(CLASS_NAME, "onRestart");
}

class UpdateTimer implements Runnable {


    public void run() {

        //Log.d(CLASS_NAME, "run");

        setTimeDisplay();

        if (handler != null) {
            handler.postDelayed(this, UPDATE_EVERY);
        }
    }
}

When I rotate the screen, the lifecycle looks like this:

Clicked Start Button
Set Buttons Enabled/Disabled
//Rotate screen
onPause
onStop
HERE ON STOP SHOULD REMOVE CALLBACKS
onDestroy
Set Buttons Enabled/Disabled
Setting Text
onStart
onResume
Set buttons enabled/disabled
re-setting time display

it seems like onDestroy is getting called when I want onResume?

È stato utile?

Soluzione

Dude when the phone rotate android will destroy your app and recreate it again...here you are right. However while this is happening the system will call onSaveInstanceState() method, so here you can save all your current instance and then get them back at on Create() method

Example:

 // to save instance before your activity is destroy
public   void onSaveInstanceState(Bundle savedInstanceState){

        super.onSaveInstanceState(savedInstanceState);
       {

        savedInstanceState.putBoolean("yourKey", timerRunning );

       }

    }
}

// then retrive it on your onCreate() method if(savedInstanceState != null){

  // Restoring all Instances

        timerRunning = savedInstanceState.getBoolean("yourKey");
  }

But if you dont like this method you can always set your activity not to recreate himself at onConfiguration Change by adding this:android:configChanges="orientation|keyboardHidden|screenSize" in your manifest inside your activity element, this method will solve also your problem and save you some time coding :)

Hope it help

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top