简体   繁体   English

如果在 onRestart() 之后调用 onStart(),是否在 onStart() 之前调用 onCreate()?

[英]Is onCreate() called before onStart() if onStart() is called after onRestart()?

I don't understand that when I release the MediaPlayer obj's resources using the release() method in the onStop() method when I resume the activity, why can the MediaPlayer still play the sound after I click the play button even when the obj was released in the onStop() method and I am sure that the onStop() method is called.我不明白当我恢复活动时使用onStop()方法中的release()方法释放MediaPlayer obj的资源时,为什么即使在obj是的情况下单击播放按钮后MediaPlayer仍然可以播放声音在onStop()方法中发布,我确定调用了onStop()方法。 So what might be the reason for this to happen, is the onCreate() method called as soon as the onRestart() method calls the onStart() method or is there any other reason for it?那么发生这种情况的原因可能是什么,是onCreate()方法在onRestart()方法调用onStart()方法后立即调用,还是有其他原因?

My logic is that if in the onStop() method I release the MediaPlayer obj's resources then there should be no instance in the obj that holds the audio file in it so when the activity is resumed no audio shall be played as the obj is assigned a null value in the releaseMediaPlayerResources() method after its resources are released so which is why I think for the obj to get an instance the onCreate() must be called as that is the method in which I have assigned the MediaPlayer obj an audio file.我的逻辑是,如果在onStop()方法中我释放了MediaPlayer obj 的资源,那么 obj 中不应该有保存音频文件的实例,因此当活动恢复时,不会播放音频,因为 obj 被分配了释放资源后releaseMediaPlayerResources()方法中的null值,这就是为什么我认为要让 obj 获取实例,必须调用onCreate() ,因为这是我为MediaPlayer obj 分配音频文件的方法。

private MediaPlayer audio;
private Button playBtn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_list_view);
    playBtn = findViewById(R.id.playBtn);
        
    playBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                releaseMediaPlayerResources();
                audio = MediaPlayer.create(this, R.raw.song);
                audio.start();
            }
    });

}
@Override
protected void onStop() {
    super.onStop();
    releaseMediaPlayerResources();
    Toast.makeText(this, "onStop() releasing res...", Toast.LENGTH_SHORT).show();
}

private void releaseMediaPlayerResources() {
    if(audio!=null){
        audio.release();
        audio = null;
    }
}

Refer to this image, it always helped me when I am wondering what function is called when?参考这张图片,当我想知道什么时候调用 function 时,它总是对我有帮助?
Android 活动生命周期

why can the MediaPlayer still play the sound after I click the play button even when the obj was released in the onStop() method?为什么即使在 onStop() 方法中释放了 obj 后,单击播放按钮后 MediaPlayer 仍然可以播放声音?

Because on each button click you first release the MediaPlayer and then create a new instance of it and start:因为在每个按钮上单击您首先释放MediaPlayer然后创建它的一个新实例并开始:

releaseMediaPlayerResources();
audio = MediaPlayer.create(YourActivity.this, R.raw.song);
audio.start();

is the onCreate() method called as soon as the onRestart() method calls the onStart() method? onCreate() 方法是否在 onRestart() 方法调用 onStart() 方法后立即调用?

No . 没有

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM