Pregunta

I started learning how to create an app on Android. I have a bit of knowledge on Java already, and now I'm trying some of the activity events.

I know how to use onCreate or onCreateOptionsMenu, and now I'm testing out onStop:

@Override
public void onStop(){
    //a function that simply creates and return an AlertDialog.Builder object
    displayPopup("Event", "Stopped", "OK").show();
}

I thought this would work since there's no error at compile time. But when I try to exit the app, I expect a popup dialog would show up but instead the app just crashed. It turns out that I need one extra line to make it work:

super.onStop();

It doesn't do anything (at least I can't see anything changed) and it's kind of useless, but without this line the app just keeps crashing.

Any help would be great, thanks.

¿Fue útil?

Solución 2

Your custom Activity extends the type Activity. The onStop() method is part of the super class activity. If you don't call super.onStop() the implementation on the Activity class is never called, and only your implementation is. This implies crashes since the onStop() method in the Activity classperforms clean ups that must be called.

The android reference on onStop() : http://developer.android.com/reference/android/app/Activity.html#onStop()

Otros consejos

It calls the onStop() method in the parent Activity class. When you look at the source code, you'll see that it does some internal housekeeping.

protected void onStop() {
    if (DEBUG_LIFECYCLE) Slog.v(TAG, "onStop " + this);
    if (mActionBar != null) mActionBar.setShowHideAnimationEnabled(false);
    getApplication().dispatchActivityStopped(this);
    mTranslucentCallback = null;
    mCalled = true;
}

Your class is an activity subclass (extends Activity), witch mean that you must call super method of the activity mother class for more information about details of super.onstop() you can check the source code

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top