A snippet from the Android Activities document(scroll down to the "foreground lifetime" line) says :
An activity can frequently transition in and out of the foreground—for example,
onPause()
is called when the device goes to sleep or when a dialog appears.
I don't quite understand this. Under what circumstances should this happen? Is onPause()
called only if the context of the dialog in question is different from the activity on top of which the dialog is to be displayed?
Going by the above-mentioned quote from document, should my activity's onPause()
method get called when the AlertDialog
(or just the Dialog
) in the following code gets displayed? Should I see the "onPause called" log entry when the dialog is displayed?
But I don't see that happen. And it shouldn't either, if I have understood the Android life cycle correctly! So, what's the document pointing at then?
public class LifeCycleTestActivity extends Activity { private static final String TAG = "LifeCycleTest"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button btn = (Button) findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Log.d(TAG, "onClick"); AlertDialog dialog = new AlertDialog.Builder(LifeCycleTestActivity.this).create(); dialog.setMessage("You Clicked on the button"); dialog.setTitle("Dialog!"); dialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { dialog.dismiss(); } }); dialog.setCancelable(true); dialog.show(); /* Dialog dialog = new Dialog(LifeCycleTestActivity.this); dialog.setTitle("Dialog!"); dialog.setCancelable(true); dialog.show(); */ } }); } @Override protected void onPause() { Log.d(TAG, "onPause() called"); super.onPause(); } @Override protected void onResume() { super.onResume(); Log.d(TAG, "onResume() called"); } }
onPause. Called when the Activity is still partially visible, but the user is probably navigating away from your Activity entirely (in which case onStop will be called next). For example, when the user taps the Home button, the system calls onPause and onStop in quick succession on your Activity .
onPause() method of an activity is call when you receive a phone call. Otherwise in lots of cases onPause() is always call with onStop() . Example like when you press home button, call another intent and more like when your activity is in background.
An activity can frequently transition in and out of the foreground—for example, onPause() is called when the device goes to sleep or when a dialog appears. Because this state can transition often, the code in these two methods should be fairly lightweight in order to avoid slow transitions that make the user wait.
Even if your activity slips into the background when another activity starts or when the screen switches off, onPause() is always called even if the other two methods aren't called. So even if activity ceases, onPause() will be called and your thread will be killed.
onPause()
is called when your activity is no longer at the top of the activity stack. A Dialog by itself is not an Activity, so will not replace the current Activity at the top of the stack, so will not cause anything to pause.
A dialog (lower-case) does not need to be implemented by a Dialog class, however. For example, it is not uncommon to implement one with an Activity whose theme is set to that of a dialog. In this case, displaying the dialog-as-an-Activity will cause the new Activity to be on the top of the stack, pausing what previously was there.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With