3

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.

Raigex
  • 1,205
  • 12
  • 32

2 Answers2

3

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
}
Damian
  • 8,062
  • 4
  • 42
  • 43
  • I am using a Nexus S and a Galaxy S for the testing. The onpause onresume onpause happens on them. But I am also working on Motorola Xoom on which it doesnt happen. – Raigex Jun 06 '12 at 21:50
  • 1
    I was testing on a Nexus One, Nexus S and a Xoom at the time. The issue only happened for me on the Nexus S so I assumed it was something to do with NFC. Using the above code in your onPause & onResume to check whether the screen is on/off should allow you to control your logic so that it executes only once. – Damian Jun 07 '12 at 09:08
  • Thanks adding the power manager to onpause worked like a charm on my nexus S. – Raigex Jun 07 '12 at 13:15
  • Well it worked but then the on Pause when I pressed the home button stoped working so this is only a partial answer – Raigex Jun 11 '12 at 19:17
0

Although this is extremely late answer, I just want to share my experience. I came across similar situation, but the main problem here is orientation changed which is caused by video player (probably playing video in landscape) or if your activity is currently in landscape. So, when your press power off button on device actually lifecycle phases are: onPause(), onStop(), then orientation is changed(lock screen is in portrait by default) which is causing calling onCreate(), onStart(), onPause()...

Solution is simple: manually handling orientation changes by specifying android:configChanges="orientation|screenSize" atribbute in your activity definition in manifest and overriding onConfigurationChanged() in java class