When I call startActivity() in onCreate(),did the other lifecycle method execute [closed]

两盒软妹~` 提交于 2020-02-24 14:11:18

问题


When I call the startActivity() method to start other activity on the onCreate() method;

Did the other lifecircle method execute,like onStart() or onResume()

I had a test

    AppMain.java

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.app_main);

            Log.i(TAG, "onCreate");
            startActivity(new Intent(AppMain.this,AppOther.class));
        }


        @Override
        protected void onRestart() {
            Log.i(TAG, "onRestart");
            super.onRestart();
        }

        @Override
        protected void onStart() {
            Log.i(TAG, "onStart");
            super.onStart();
        }

        @Override
        protected void onRestoreInstanceState(Bundle savedInstanceState) {
            Log.i(TAG, "onRestoreInstanceState");
            super.onRestoreInstanceState(savedInstanceState);
        }

        @Override
        protected void onResume() {
            Log.i(TAG, "onResume");
            super.onResume();
        }


        @Override
        protected void onPause() {
            Log.i(TAG, "onPause");
            super.onPause();
        }

        @Override
        protected void onSaveInstanceState(Bundle outState) {
            Log.i(TAG, "onSaveInstanceState");
            super.onSaveInstanceState(outState);
        }

        @Override
        protected void onStop() {
            Log.i(TAG, "onStop");
            super.onStop();
        }



        @Override
        protected void onDestroy() {
            Log.i(TAG, "onDestroy");
            super.onDestroy();
        }


        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            Log.i(TAG, "onConfigurationChanged");
            super.onConfigurationChanged(newConfig);
        }

    AppOther.java

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.app_other);

        Log.i(TAG, "onCreate");

    }

    @Override
    protected void onRestart() {
        Log.i(TAG, "onRestart");
        super.onRestart();
    }

    @Override
    protected void onStart() {
        Log.i(TAG, "onStart");
        super.onStart();
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        Log.i(TAG, "onRestoreInstanceState");
        super.onRestoreInstanceState(savedInstanceState);
    }

    @Override
    protected void onResume() {
        Log.i(TAG, "onResume");

        super.onResume();
    }


    @Override
    protected void onPause() {
        Log.i(TAG, "onPause");

        super.onPause();
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        Log.i(TAG, "onSaveInstanceState");
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStop() {
        Log.i(TAG, "onStop");
        super.onStop();
    }



    @Override
    protected void onDestroy() {
        Log.i(TAG, "onDestroy");
        super.onDestroy();
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        Log.i(TAG, "onConfigurationChanged");
        super.onConfigurationChanged(newConfig);
    }

Logcat:

05-29 05:28:11.583: I/AppMain(1257): onCreate
05-29 05:28:11.614: I/AppMain(1257): onStart
05-29 05:28:11.614: I/AppMain(1257): onResume
05-29 05:28:11.643: I/AppMain(1257): onSaveInstanceState
05-29 05:28:11.643: I/AppMain(1257): onPause
05-29 05:28:11.793: I/AppOther(1257): onCreate
05-29 05:28:11.793: I/AppOther(1257): onStart
05-29 05:28:11.793: I/AppOther(1257): onResume
05-29 05:28:12.383: I/AppMain(1257): onStop

I don't know why the onStart() and onResume() method can still execute; It's seems that startActivity() did not break the AppMain's lifecycle


回答1:


Language is pale, speak in code. Just test it.

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.v(this.getClass().toString(),"onCreate");
    //start other Activity
    this.startActivity(new Intent(this,OtherActivity.class));
}

@Override
protected void onDestroy() {
    super.onDestroy();
    Log.v(this.getClass().toString(),"onDestroy");
}

@Override
protected void onPause() {
    super.onPause();
    Log.v(this.getClass().toString(),"onPause");
}

@Override
protected void onResume() {
    super.onResume();
    Log.v(this.getClass().toString(),"onResume");
}

@Override
protected void onStart() {
    super.onStart();
    Log.v(this.getClass().toString(),"onStart");
}

}

And the logcat shows:




回答2:


Question's way too narrow but if you start another activity, the current activity will toggle onPause(). and if you finished the called activity the previous activity will toggle onResume(). In short it will not call onResume() and onStart() of current activity if you start another acitivity.



来源:https://stackoverflow.com/questions/23925333/when-i-call-startactivity-in-oncreate-did-the-other-lifecycle-method-execute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!