Reputation: 1457
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.
Upvotes: 4
Views: 5448
Reputation: 215
onResume IS called at startup. Please refer to the the activity lifecycle documentation here.
Upvotes: 0
Reputation: 22245
I think you will find the recent updates to the Android developer documentation will clarify your answer. onResume() will be called anytime your activity starts for the first time, bring the activity to the foreground, or bring the phone out of the lock screen.
Android Activity Documentation
Upvotes: 0
Reputation: 55009
onResume isn't limited to being called after the activity has been paused, it's called whenever the activity goes to the top of the activity stack. That includes the first time it's shown after it's been created.
The developer doucmentation is quite detailed about how the actiivty lifecycle works, including a flowchart and table which describes when each lifecycle callback is actually called.
Upvotes: 4
Reputation: 1006674
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.
Upvotes: 5