Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Lifecycle: Is onResume() supposed to be called during startup?

I'm trying an example from Android Application Development for Dummies, which is a simple app that toggles the ringing mode of the phone. The code is below.

public class SilentModeToggleActivity extends Activity {

  private AudioManager mAudioManager;
  private boolean mPhoneIsSilent;

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    mAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
    checkIfPhoneIsSilent();
    setButtonClickListener();
  }

  @Override public void onResume() {
    super.onResume();
    checkIfPhoneIsSilent();
    toggleUi();
  }

  private void checkIfPhoneIsSilent() {
    int ringerMode = mAudioManager.getRingerMode();
    if (ringerMode == AudioManager.RINGER_MODE_SILENT) {
      mPhoneIsSilent = true;
    } else {
      mPhoneIsSilent = false;
    }
  }

  private void setButtonClickListener() {
    Button toggleButton = (Button) findViewById(R.id.toggleButton);
    toggleButton.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
        if (mPhoneIsSilent) {
          mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
          mPhoneIsSilent = false;
        } else {
          mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
          mPhoneIsSilent = true;
        }
        toggleUi();
      }
    });
  }

  private void toggleUi() {
    ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
    Drawable newPhoneImage;
    if (mPhoneIsSilent) {
      newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
    } else {
      newPhoneImage = getResources().getDrawable(R.drawable.phone_on);
    }
    imageView.setImageDrawable(newPhoneImage);
  }
}

My question is since I only override onCreate() (shows the "normal" mode image at default) and onResume(), it's expected that the image changes to "silent" if I change the phone mode to silent outside the app (in onResume() I check the current state and update the UI), but why it still shows the correct image even if I KILL the process and then change the phone mode to silent?

I would expect the app to reload and shows the default image, which is normal. Not the correct yet confusing silent image.

like image 633
user1447343 Avatar asked Jan 17 '23 11:01

user1447343


1 Answers

onResume() is called any time an activity is regaining the foreground input. This includes:

  • When it is returning to the screen after something else had the foreground (e.g., Settings), and

  • When it is being created for the first time in this process (which includes any new process required because you killed the old one from DDMS)

Hence, your code will examine the state of the ringer mode in either case and will use the proper image in either case.

like image 129
CommonsWare Avatar answered Jan 25 '23 15:01

CommonsWare