Search code examples
androidonresumeonpause

Activity onPause weird action


I have an onPause in my activity. The weird thing is that in just that single activity (a videoPlayer) onPause gets called twice

Scenario: This happens in my video player on phone devices but not tablets.

Press Power Button to go to standby, onpause is called then onresume is called then on pause is called again. then press power button again and on resume is called like it is supposed to.

Has anyone ever encountered such a problem and if so how did you fix it. Even just having onPause onResume onpause then power on onResume.

Thanks in advance. this one appears twice

    protected void onPause()
{

    Log.i("VideoPlayer", "onPauseCalled");
    super.onPause();
    pauseMedia();
    Log.i("onPause", " save states to be called");
    if(saveAllowed)
        saveStates();
    Log.i("onPause", " save States called");
    view.setVisibility(View.GONE);
    //Log.i("onPause", "visibility GOne");
    removeListeners();
    doCleanUp();


}

    @Override
protected void onResume()
{
    super.onResume();
    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    Log.i("VideoPlayer", "onResumeCalled");

    if(pm.isScreenOn())
    {
        //Initialization
        initializeViewControls();
        handler = new Handler();

        initializeButtons();
        initRecordButtons();
        initVolumeControl();

        //RestoreState
        restoreMediaBoolean();
        restoreMediaState();
        Log.i("After", "restoreState");
        view = (SurfaceView) findViewById(R.id.surfaceView);
        //Log.i("After", "GettingView");
        holder = view.getHolder();
        view.setVisibility(View.VISIBLE);
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        Log.i("onResume", "view is Visible");
        saveAllowed = true;
    }
    else
        saveAllowed = false;

}

private void saveStates()
{
    if(!isFinishing)
    {
        //Log.i("VideoPlayer", "Called at saveStates()");

        prefs = getSharedPreferences(PREF_SAVE, Context.MODE_PRIVATE);
        if(prefs != null)
        {
            prefEditor = prefs.edit();
        }

        if(prefEditor != null)
        {

            //Save overlay visibiltiy
            if((volumeLayout != null) && (volumeLayout.getVisibility() == View.VISIBLE))
            {
                prefEditor.putBoolean(IS_VOLUME_VISIBLE, true);
                prefEditor.commit();
            }
            else
            {
                prefEditor.putBoolean(IS_VOLUME_VISIBLE, false);
                prefEditor.commit();
            }

            //Save recorder Overlay state
            if((recordLayout != null) && (recordLayout.getVisibility() == View.VISIBLE) )
            {
                prefEditor.putBoolean(IS_RECORDER_VISIBLE, true);
                prefEditor.commit();
            }
            else
            {
                prefEditor.putBoolean(IS_RECORDER_VISIBLE, false);
                prefEditor.commit();
            }
            //Save controller OVerlay State
            if((backButton != null) && (backButton.getVisibility() == View.VISIBLE))
            {

                prefEditor.putBoolean(IS_CONTROLLER_VISIBLE, true);
                prefEditor.commit();
                Log.i("Save States", "Controller saved as visible");
            }
            else
            {
                Log.i("Save States", "Controller saved as Invisible");
                //Log.i("Saving", "Controller Invisible");
                prefEditor.putBoolean(IS_CONTROLLER_VISIBLE, false);
                prefEditor.commit();
            }

            //Save is private Audio Recorded
            prefEditor.putBoolean(IS_PRIVATE_AUDIO_RECORDED, isCustomVoiceRecorded);
            prefEditor.commit();

            //Save are we playing out custom audio
            prefEditor.putBoolean(PLAY_CUSTOM_AUDIO, playCustomAudio);
            prefEditor.commit();

            //Make boolean is restoring
            prefEditor.putBoolean(IS_RESTORING, true);
            prefEditor.commit();

            //Saves Position of Current Video
            if(vidplayer != null)
            {
                prefEditor.putInt(VIDEO_POSITION, videoPausedAt);
                prefEditor.commit();
            }

            //Save Position of Current Audio
            if(audplayer != null)
            {
                prefEditor.putInt(AUDIO_POSITION, audioPausedAt);
                prefEditor.commit();
            }


            prefEditor = null;
            prefs = null;

        }
    }

What happens is that i save the state of some layouts like a recorder layout which pauses while it is out kinda like a menu. Then if power is pressed, it turns of but when it turns back on the layouts are gone and the video is playing which is not supposed to happen.


Solution

  • Yes, I had a similar problem. Which device are you using (I think it was an NFC screen timeout issue if memory serves me correctly)? Pause wasn't an issue for me but I try to resume the video in onResume (which gets called twice) so I used the following in onResume:

    PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
    if (pm.isScreenOn()) {
       //now do stuff
    }