Java 欠债可能没有意义,或者不是一种好的做法,但它只是为了证明一点(人们可能会发现更真实的情况)。创建主活动,该活动在结束时和最后一个活动时应转到“再见”活动: @Override protected void onDestroy() { super.onDestroy(); if(((SherifApplication) getApplication()).getCount() == 0) { //i want to go to a certain activity when there are no other activities startActivity(new Intent(this, GoodBye.class)); } }

Java 欠债可能没有意义,或者不是一种好的做法,但它只是为了证明一点(人们可能会发现更真实的情况)。创建主活动,该活动在结束时和最后一个活动时应转到“再见”活动: @Override protected void onDestroy() { super.onDestroy(); if(((SherifApplication) getApplication()).getCount() == 0) { //i want to go to a certain activity when there are no other activities startActivity(new Intent(this, GoodBye.class)); } },java,android,android-lifecycle,Java,Android,Android Lifecycle,如果在onDestroy开始时调用super.onDestroy,将启动“再见”活动。如果在onDestroy结束时调用super.onDestroy,则不会启动“再见”活动 当然,同样,这不是最佳的例子。然而,这表明谷歌在这方面有点混乱。任何其他变量都不会影响应用程序的行为。然而,将这些分派添加到onDestroy会导致super以某种方式干扰您的子类 我说他们捣乱的原因也不一样。他们(在api 14之前)不仅在超级调用中只涉及final和/或private,而且还调用了不同的内部函数(pri

如果在onDestroy开始时调用super.onDestroy,将启动“再见”活动。如果在onDestroy结束时调用super.onDestroy,则不会启动“再见”活动

当然,同样,这不是最佳的例子。然而,这表明谷歌在这方面有点混乱。任何其他变量都不会影响应用程序的行为。然而,将这些分派添加到onDestroy会导致super以某种方式干扰您的子类

我说他们捣乱的原因也不一样。他们(在api 14之前)不仅在超级调用中只涉及final和/或private,而且还调用了不同的内部函数(private),然后实际调度onPause。。。功能

例如,
performStop
函数是被调用的函数,它依次调用onStop函数:

final void performStop() {
    if (mLoadersStarted) {
        mLoadersStarted = false;
        if (mLoaderManager != null) {
            if (!mChangingConfigurations) {
                mLoaderManager.doStop();
            } else {
                mLoaderManager.doRetain();
            }
        }
    }

    if (!mStopped) {
        if (mWindow != null) {
            mWindow.closeAllPanels();
        }

        if (mToken != null && mParent == null) {
            WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
        }

        mFragments.dispatchStop();

        mCalled = false;
        mInstrumentation.callActivityOnStop(this);
        if (!mCalled) {
            throw new SuperNotCalledException(
                    "Activity " + mComponent.toShortString() +
                    " did not call through to super.onStop()");
        }

        synchronized (mManagedCursors) {
            final int N = mManagedCursors.size();
            for (int i=0; i<N; i++) {
                ManagedCursor mc = mManagedCursors.get(i);
                if (!mc.mReleased) {
                    mc.mCursor.deactivate();
                    mc.mReleased = true;
                }
            }
        }

        mStopped = true;
    }
    mResumed = false;
}
final void performStop(){
如果(mLoaderStarted){
mloaderstarted=false;
if(mLoaderManager!=null){
如果(!mChangingConfigurations){
mLoaderManager.doStop();
}否则{
mLoaderManager.doRetain();
}
}
}
如果(!mStopped){
如果(mWindow!=null){
mWindows.关闭所有面板();
}
if(mToken!=null&&mParent==null){
WindowManagerGlobal.getInstance().setStoppedState(mToken,true);
}
mffragments.dispatchStop();
mcall=false;
Minstrurement.callActivityOnStop(此);
如果(!mcall){
抛出新的SuperNotCalledException(
“活动”+mComponent.toShortString()+
“未通过调用super.onStop()”;
}
已同步(mManagedCursors){
final int N=mManagedCursors.size();

对于(inti=0;i,从java的角度来看,这里有一些解决方案:

需要在子类构造函数之前调用父类构造函数。这将确保如果在构造函数中调用父类上的任何方法,则父类已正确设置

您试图做的是,将参数传递给超级构造函数是完全合法的,您只需要在执行操作时内联构造这些参数,或者将它们传递给构造函数,然后将它们传递给超级构造函数:

public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                super(myArray);
        }
}
如果编译器未强制执行此操作,则可以执行以下操作:

public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                someMethodOnSuper(); //ERROR super not yet constructed
                super(myArray);
        }
}
它表明,实际上,子字段必须在SuperClass之前进行初始化!同时,java需求通过专门化super构造函数参数来“保护”我们不专门化类

在父类具有默认构造函数的情况下,编译器会自动为您插入对super的调用。由于Java中的每个类都继承自Object,因此必须以某种方式调用Object构造函数,并且必须首先执行它。super()的自动插入编译器允许这样做。强制super首先出现,强制构造函数体按照正确的顺序执行,即:Object->Parent->Child->ChildOfChild->SoOnSoForth

(1) 检查super是否是第一条语句不足以防止出现此问题。例如,您可以在构造函数中放置“super(someMethodInSuper());”。这会尝试在构造超类中的方法之前访问该方法,即使super是第一条语句

(2) 编译器似乎实现了一个不同的检查,这本身就足以防止此问题。消息是“在调用超类型构造函数之前无法引用xxx”。因此,检查super是否是第一条语句是没有必要的

请检查一下

首先销毁特定于实例的资源,然后再销毁 实例特定资源可能依赖的超类资源 这是有道理的,而不是相反。但评论表明 否则。我错过了什么?

在我看来:没有一件事

Mark(又名Commonware)的回答揭示了这一问题:。但是,你可以在他的回答上看到以下评论:

但是为什么官方文档在onPause()中说:“始终首先调用超类方法”

回到第一步。好的,让我们从另一个角度来看这一点。我们知道Java语言规范没有指定调用
super.overrideMethod()
的顺序(或者如果必须进行调用)

在类活动的情况下,需要并强制执行
super.overrideMethod()
调用:

Activity.onStop()
中的
mCalled
设置为true

现在,唯一需要讨论的细节是顺序

我也知道这两种方法都有效

当然可以。查看Activity.onPause()的方法体:

无论您以何种方式夹心调用
super.onPause()
,都不会有问题。Activity.onStop()有一个类似的方法体。但请看一下Activity.onDestroy():

并且,来自android sdk中包含的LunarLander示例应用程序:

public class LunarLander extends Activity {

    ....

    @Override
    protected void onPause() {
        mLunarView.getThread().pause(); // pause game when Activity pauses
        super.onPause();
    }
    ....
}
总结和值得一提的内容:

用户Philip Sheard:提供了一种场景,如果使用
startActivityForResult(Intent)
启动活动,则必须延迟调用
super.onPause()
。在
super.onPause()之后使用
setResult(…)
设置结果
不起作用。他稍后在回答的评论中对此进行了澄清

用户Sherif elKhatib:解释为什么让超类首先初始化其资源,然后销毁其资源
package mobi.shush;

import android.app.Activity;
import android.app.Application;
import android.app.Application.ActivityLifecycleCallbacks;
import android.os.Bundle;

public class SherifApplication extends Application implements ActivityLifecycleCallbacks {
    @Override
    public void onCreate() {
        super.onCreate();
        registerActivityLifecycleCallbacks(this);
    }
    public int getCount() {
        return count;
    }
    int count = 0;
    @Override
    public void onActivityCreated(Activity activity, Bundle savedInstanceState) {
        count++;
    }
    @Override
    public void onActivityDestroyed(Activity activity) {
        count--;
    }
    @Override
    public void onActivityPaused(Activity activity) {}
    @Override
    public void onActivityResumed(Activity activity) {}
    @Override
    public void onActivitySaveInstanceState(Activity activity, Bundle outState)           {}
    @Override
    public void onActivityStarted(Activity activity) {}
    @Override
    public void onActivityStopped(Activity activity) {}
}
@Override
protected void onDestroy() {
    super.onDestroy();
    if(((SherifApplication) getApplication()).getCount() == 0) {
        //i want to go to a certain activity when there are no other activities
        startActivity(new Intent(this, GoodBye.class));
    }
}
final void performStop() {
    if (mLoadersStarted) {
        mLoadersStarted = false;
        if (mLoaderManager != null) {
            if (!mChangingConfigurations) {
                mLoaderManager.doStop();
            } else {
                mLoaderManager.doRetain();
            }
        }
    }

    if (!mStopped) {
        if (mWindow != null) {
            mWindow.closeAllPanels();
        }

        if (mToken != null && mParent == null) {
            WindowManagerGlobal.getInstance().setStoppedState(mToken, true);
        }

        mFragments.dispatchStop();

        mCalled = false;
        mInstrumentation.callActivityOnStop(this);
        if (!mCalled) {
            throw new SuperNotCalledException(
                    "Activity " + mComponent.toShortString() +
                    " did not call through to super.onStop()");
        }

        synchronized (mManagedCursors) {
            final int N = mManagedCursors.size();
            for (int i=0; i<N; i++) {
                ManagedCursor mc = mManagedCursors.get(i);
                if (!mc.mReleased) {
                    mc.mCursor.deactivate();
                    mc.mReleased = true;
                }
            }
        }

        mStopped = true;
    }
    mResumed = false;
}
public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                super(myArray);
        }
}
public MySubClassB extends MyClass {
        public MySubClassB(Object[] myArray) {
                someMethodOnSuper(); //ERROR super not yet constructed
                super(myArray);
        }
}
if (!mCalled) {
    throw new SuperNotCalledException(
        "Activity " + mComponent.toShortString() +
            " did not call through to super.onStop()");
}
protected void onPause() {
    if (DEBUG_LIFECYCLE) Slog.v(TAG, "onPause " + this);

    // This is to invoke 
    // Application.ActivityLifecyleCallbacks.onActivityPaused(Activity)
    getApplication().dispatchActivityPaused(this);

    // The flag to enforce calling of this method
    mCalled = true;
}
protected void onDestroy() {
    if (DEBUG_LIFECYCLE) Slog.v(TAG, "onDestroy " + this);
    mCalled = true;

    // dismiss any dialogs we are managing.
    if (mManagedDialogs != null) {
        final int numDialogs = mManagedDialogs.size();
        for (int i = 0; i < numDialogs; i++) {
            final ManagedDialog md = mManagedDialogs.valueAt(i);
            if (md.mDialog.isShowing()) {
                md.mDialog.dismiss();
            }
        }
        mManagedDialogs = null;
    }

    // close any cursors we are managing.
    synchronized (mManagedCursors) {
        int numCursors = mManagedCursors.size();
        for (int i = 0; i < numCursors; i++) {
            ManagedCursor c = mManagedCursors.get(i);
            if (c != null) {
                c.mCursor.close();
            }
        }
        mManagedCursors.clear();
    }

    // Close any open search dialog
    if (mSearchManager != null) {
        mSearchManager.stopSearch();
    }

    getApplication().dispatchActivityDestroyed(this);
}
public class ListActivity extends Activity {

    ....

    @Override
    protected void onDestroy() {
        mHandler.removeCallbacks(mRequestFocus);
        super.onDestroy();
    }
    ....    
}
public class LunarLander extends Activity {

    ....

    @Override
    protected void onPause() {
        mLunarView.getThread().pause(); // pause game when Activity pauses
        super.onPause();
    }
    ....
}