[Android] Remove listeners when video player has no focus

Geoffrey Métais git at videolan.org
Tue May 12 11:10:33 CEST 2015


vlc-ports/android | branch: master | Geoffrey Métais <geoffrey.metais at gmail.com> | Tue May 12 11:08:58 2015 +0200| [12d52744f6a5422dcd529206ff185801d92ff856] | committer: Geoffrey Métais

Remove listeners when video player has no focus

May prevent NPE from listeners trying to access VideoPlayerActivity instance while closing.

> http://git.videolan.org/gitweb.cgi/vlc-ports/android.git/?a=commit;h=12d52744f6a5422dcd529206ff185801d92ff856
---

 .../vlc/gui/video/VideoPlayerActivity.java         |   45 ++++++++++++--------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
index 44535d2..b21224a 100644
--- a/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
+++ b/vlc-android/src/org/videolan/vlc/gui/video/VideoPlayerActivity.java
@@ -407,9 +407,7 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
 
         // Position and remaining time
         mTime = (TextView) findViewById(R.id.player_overlay_time);
-        mTime.setOnClickListener(mRemainingTimeListener);
         mLength = (TextView) findViewById(R.id.player_overlay_length);
-        mLength.setOnClickListener(mRemainingTimeListener);
 
         // the info textView is not on the overlay
         mInfo = (TextView) findViewById(R.id.player_overlay_info);
@@ -421,15 +419,12 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
                 mSettings.getString("screen_orientation_value", "4" /*SCREEN_ORIENTATION_SENSOR*/));
 
         mPlayPause = (ImageView) findViewById(R.id.player_overlay_play);
-        mPlayPause.setOnClickListener(mPlayPauseListener);
 
         mTracks = (ImageView) findViewById(R.id.player_overlay_tracks);
         mAdvOptions = (ImageView) findViewById(R.id.player_overlay_adv_function);
         mLock = (ImageView) findViewById(R.id.lock_overlay_button);
-        mLock.setOnClickListener(mLockListener);
 
         mSize = (ImageView) findViewById(R.id.player_overlay_size);
-        mSize.setOnClickListener(mSizeListener);
 
         mDelayPlus = (ImageView) findViewById(R.id.player_delay_plus);
         mDelayMinus = (ImageView) findViewById(R.id.player_delay_minus);
@@ -455,7 +450,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
         }
 
         mSeekbar = (SeekBar) findViewById(R.id.player_overlay_seekbar);
-        mSeekbar.setOnSeekBarChangeListener(mSeekListener);
 
         /* Loading view */
         mLoading = (ImageView) findViewById(R.id.player_overlay_loading);
@@ -544,9 +538,37 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
                 return super.onOptionsItemSelected(item);
         }
     }
+
+    @Override
+    protected void onResume() {
+        super.onResume();
+        mSwitchingView = false;
+
+        /*
+         * Set listeners here to avoid NPE when activity is closing
+         */
+        mSeekbar.setOnSeekBarChangeListener(mSeekListener);
+        mLock.setOnClickListener(mLockListener);
+        mPlayPause.setOnClickListener(mPlayPauseListener);
+        mLength.setOnClickListener(mRemainingTimeListener);
+        mTime.setOnClickListener(mRemainingTimeListener);
+        mSize.setOnClickListener(mSizeListener);
+
+        bindAudioService();
+
+        if (mIsLocked && mScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR)
+            setRequestedOrientation(mScreenOrientationLock);
+    }
+
     @Override
     protected void onPause() {
         super.onPause();
+        mSeekbar.setOnSeekBarChangeListener(null);
+        mLock.setOnClickListener(null);
+        mPlayPause.setOnClickListener(null);
+        mLength.setOnClickListener(null);
+        mTime.setOnClickListener(null);
+        mSize.setOnClickListener(null);
 
         /* Stop the earliest possible to avoid vout error */
         if (isFinishing())
@@ -647,17 +669,6 @@ public class VideoPlayerActivity extends AppCompatActivity implements IVideoPlay
         mBound = false;
     }
 
-    @Override
-    protected void onResume() {
-        super.onResume();
-        mSwitchingView = false;
-
-        bindAudioService();
-
-        if (mIsLocked && mScreenOrientation == ActivityInfo.SCREEN_ORIENTATION_SENSOR)
-            setRequestedOrientation(mScreenOrientationLock);
-    }
-
     /**
      * Add or remove MediaRouter callbacks. This is provided for version targeting.
      *



More information about the Android mailing list