Skip to content

Commit 7f1aefe

Browse files
committed
remove onBackPressed
1 parent 8823862 commit 7f1aefe

33 files changed

+448
-450
lines changed

app/src/main/java/com/example/gsyvideoplayer/AudioDetailPlayer.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import android.os.Bundle;
55
import android.view.View;
66

7+
import androidx.activity.OnBackPressedCallback;
78
import androidx.annotation.NonNull;
89
import androidx.appcompat.app.AppCompatActivity;
910
import androidx.media3.exoplayer.SeekParameters;
@@ -142,24 +143,21 @@ public void onClick(View v) {
142143
binding.detailPlayer.startWindowFullscreen(AudioDetailPlayer.this, true, true);
143144
}
144145
});
145-
}
146-
147-
@Override
148-
public void onBackPressed() {
149-
150-
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
151-
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
152-
if (orientationUtils != null) {
153-
orientationUtils.backToProtVideo();
154-
}
155146

156-
if (GSYVideoManager.backFromWindowFull(this)) {
157-
return;
158-
}
159-
super.onBackPressed();
147+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
148+
@Override
149+
public void handleOnBackPressed() {
150+
if (orientationUtils != null) {
151+
orientationUtils.backToProtVideo();
152+
}
153+
if (GSYVideoManager.backFromWindowFull(AudioDetailPlayer.this)) {
154+
return;
155+
}
156+
finish();
157+
}
158+
});
160159
}
161160

162-
163161
@Override
164162
protected void onPause() {
165163
getCurPlay().onVideoPause();

app/src/main/java/com/example/gsyvideoplayer/AutoPlayRecyclerViewActivity.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import android.view.View;
1010
import android.view.Window;
1111

12+
import androidx.activity.OnBackPressedCallback;
1213
import androidx.annotation.NonNull;
1314
import androidx.appcompat.app.AppCompatActivity;
1415
import androidx.recyclerview.widget.LinearLayoutManager;
@@ -95,6 +96,15 @@ public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
9596
}
9697
});
9798

99+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
100+
@Override
101+
public void handleOnBackPressed() {
102+
if (GSYVideoManager.backFromWindowFull(AutoPlayRecyclerViewActivity.this)) {
103+
return;
104+
}
105+
finish();
106+
}
107+
});
98108
}
99109

100110
@Override
@@ -105,14 +115,6 @@ public void onConfigurationChanged(@NonNull Configuration newConfig) {
105115

106116
}
107117

108-
@Override
109-
public void onBackPressed() {
110-
if (GSYVideoManager.backFromWindowFull(this)) {
111-
return;
112-
}
113-
super.onBackPressed();
114-
}
115-
116118
@Override
117119
protected void onPause() {
118120
super.onPause();

app/src/main/java/com/example/gsyvideoplayer/DanmkuVideoActivity.java

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.view.View;
77
import android.widget.ImageView;
88

9+
import androidx.activity.OnBackPressedCallback;
910
import androidx.annotation.NonNull;
1011
import androidx.appcompat.app.AppCompatActivity;
1112

@@ -137,24 +138,20 @@ public void onClick(View view, boolean lock) {
137138
}
138139
});
139140

141+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
142+
@Override
143+
public void handleOnBackPressed() {
144+
if (orientationUtils != null) {
145+
orientationUtils.backToProtVideo();
146+
}
147+
if (GSYVideoManager.backFromWindowFull(DanmkuVideoActivity.this)) {
148+
return;
149+
}
150+
finish();
151+
}
152+
});
140153
}
141154

142-
@Override
143-
public void onBackPressed() {
144-
145-
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
146-
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
147-
if (orientationUtils != null) {
148-
orientationUtils.backToProtVideo();
149-
}
150-
151-
if (GSYVideoManager.backFromWindowFull(this)) {
152-
return;
153-
}
154-
super.onBackPressed();
155-
}
156-
157-
158155
@Override
159156
protected void onPause() {
160157
getCurPlay().onVideoPause();

app/src/main/java/com/example/gsyvideoplayer/DetailDownloadExoPlayer.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import android.widget.ImageView;
88
import android.widget.Toast;
99

10+
import androidx.activity.OnBackPressedCallback;
1011
import androidx.annotation.NonNull;
1112
import androidx.annotation.Nullable;
1213
import androidx.appcompat.app.AppCompatActivity;
@@ -196,25 +197,22 @@ public void onClick(View v) {
196197
}
197198
});
198199

199-
200-
}
201-
202-
@Override
203-
public void onBackPressed() {
204-
205-
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
206-
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
207-
if (orientationUtils != null) {
208-
orientationUtils.backToProtVideo();
209-
}
210-
211-
if (GSYVideoManager.backFromWindowFull(this)) {
212-
return;
213-
}
214-
super.onBackPressed();
200+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
201+
@Override
202+
public void handleOnBackPressed() {
203+
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
204+
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
205+
if (orientationUtils != null) {
206+
orientationUtils.backToProtVideo();
207+
}
208+
if (GSYVideoManager.backFromWindowFull(DetailDownloadExoPlayer.this)) {
209+
return;
210+
}
211+
finish();
212+
}
213+
});
215214
}
216215

217-
218216
@Override
219217
protected void onPause() {
220218
getCurPlay().onVideoPause();

app/src/main/java/com/example/gsyvideoplayer/DetailDownloadPlayer.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.widget.ImageView;
77
import android.widget.Toast;
88

9+
import androidx.activity.OnBackPressedCallback;
910
import androidx.annotation.NonNull;
1011
import androidx.appcompat.app.AppCompatActivity;
1112
import androidx.media3.exoplayer.SeekParameters;
@@ -178,25 +179,22 @@ public void onClick(View v) {
178179

179180
proxyCacheServer = ProxyCacheManager.instance().newProxy(getApplicationContext());
180181

181-
182-
}
183-
184-
@Override
185-
public void onBackPressed() {
186-
187-
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
188-
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
189-
if (orientationUtils != null) {
190-
orientationUtils.backToProtVideo();
191-
}
192-
193-
if (GSYVideoManager.backFromWindowFull(this)) {
194-
return;
195-
}
196-
super.onBackPressed();
182+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
183+
@Override
184+
public void handleOnBackPressed() {
185+
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
186+
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
187+
if (orientationUtils != null) {
188+
orientationUtils.backToProtVideo();
189+
}
190+
if (GSYVideoManager.backFromWindowFull(DetailDownloadPlayer.this)) {
191+
return;
192+
}
193+
finish();
194+
}
195+
});
197196
}
198197

199-
200198
@Override
201199
protected void onPause() {
202200
getCurPlay().onVideoPause();

app/src/main/java/com/example/gsyvideoplayer/DetailFilterActivity.java

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import android.opengl.Matrix;
99
import android.os.Bundle;
1010

11+
import androidx.activity.OnBackPressedCallback;
1112
import androidx.annotation.NonNull;
1213

1314
import android.util.Log;
@@ -176,7 +177,7 @@ public void onClick(View v) {
176177
binding.detailPlayer.setBackFromFullScreenListener(new View.OnClickListener() {
177178
@Override
178179
public void onClick(View v) {
179-
DetailFilterActivity.this.onBackPressed();
180+
getOnBackPressedDispatcher().onBackPressed();
180181
}
181182
});
182183

@@ -233,6 +234,20 @@ public void onClick(View v) {
233234
//do nothing
234235
}
235236
});
237+
238+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
239+
@Override
240+
public void handleOnBackPressed() {
241+
if (orientationUtils != null) {
242+
orientationUtils.backToProtVideo();
243+
}
244+
if (getGSYVideoPlayer().isIfCurrentIsFullscreen()) {
245+
getGSYVideoPlayer().getFullscreenButton().performClick();
246+
return;
247+
}
248+
finish();
249+
}
250+
});
236251
}
237252

238253
@Override

app/src/main/java/com/example/gsyvideoplayer/DetailMoreTypeActivity.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.view.View;
77
import android.widget.ImageView;
88

9+
import androidx.activity.OnBackPressedCallback;
910
import androidx.annotation.NonNull;
1011
import androidx.appcompat.app.AppCompatActivity;
1112

@@ -145,24 +146,21 @@ public void onClick(View view, boolean lock) {
145146
});
146147

147148
loadFirstFrameCover(source1);
148-
}
149-
150-
@Override
151-
public void onBackPressed() {
152149

153-
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
154-
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
155-
if (orientationUtils != null) {
156-
orientationUtils.backToProtVideo();
157-
}
158-
159-
if (GSYVideoManager.backFromWindowFull(this)) {
160-
return;
161-
}
162-
super.onBackPressed();
150+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
151+
@Override
152+
public void handleOnBackPressed() {
153+
if (orientationUtils != null) {
154+
orientationUtils.backToProtVideo();
155+
}
156+
if (GSYVideoManager.backFromWindowFull(DetailMoreTypeActivity.this)) {
157+
return;
158+
}
159+
finish();
160+
}
161+
});
163162
}
164163

165-
166164
@Override
167165
protected void onPause() {
168166
getCurPlay().onVideoPause();

app/src/main/java/com/example/gsyvideoplayer/DetailNormalActivityPlayer.java

Lines changed: 15 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
package com.example.gsyvideoplayer;
22

3-
import android.app.Activity;
43
import android.content.res.Configuration;
54
import android.os.Bundle;
65
import android.view.View;
76
import android.widget.ImageView;
87

8+
import androidx.activity.OnBackPressedCallback;
9+
import androidx.appcompat.app.AppCompatActivity;
910
import androidx.media3.exoplayer.SeekParameters;
1011

1112
import com.example.gsyvideoplayer.databinding.ActivityDetailPlayerBinding;
@@ -24,7 +25,7 @@
2425
import tv.danmaku.ijk.media.exo2.Exo2PlayerManager;
2526

2627

27-
public class DetailNormalActivityPlayer extends Activity {
28+
public class DetailNormalActivityPlayer extends AppCompatActivity {
2829

2930

3031
private boolean isPlay;
@@ -148,24 +149,21 @@ public void onClick(View v) {
148149
binding.detailPlayer.startWindowFullscreen(DetailNormalActivityPlayer.this, false, true);
149150
}
150151
});
151-
}
152-
153-
@Override
154-
public void onBackPressed() {
155152

156-
// ------- !!!如果不需要旋转屏幕,可以不调用!!!-------
157-
// 不需要屏幕旋转,还需要设置 setNeedOrientationUtils(false)
158-
if (orientationUtils != null) {
159-
orientationUtils.backToProtVideo();
160-
}
161-
162-
if (GSYVideoManager.backFromWindowFull(this)) {
163-
return;
164-
}
165-
super.onBackPressed();
153+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
154+
@Override
155+
public void handleOnBackPressed() {
156+
if (orientationUtils != null) {
157+
orientationUtils.backToProtVideo();
158+
}
159+
if (GSYVideoManager.backFromWindowFull(DetailNormalActivityPlayer.this)) {
160+
return;
161+
}
162+
finish();
163+
}
164+
});
166165
}
167166

168-
169167
@Override
170168
protected void onPause() {
171169
getCurPlay().onVideoPause();

app/src/main/java/com/example/gsyvideoplayer/FragmentVideoActivity.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.os.Bundle;
77
import android.transition.Explode;
88
import android.view.Window;
9+
import androidx.activity.OnBackPressedCallback;
910

1011
import com.example.gsyvideoplayer.fragment.VideoFragment;
1112

@@ -28,13 +29,15 @@ protected void onCreate(Bundle savedInstanceState) {
2829
transaction.replace(R.id.frameLayout, newFragment);
2930
transaction.addToBackStack(null);
3031
transaction.commit();
31-
}
3232

33-
@Override
34-
public void onBackPressed() {
35-
if (newFragment.onBackPressed()) {
36-
return;
37-
}
38-
finish();
33+
getOnBackPressedDispatcher().addCallback(this, new OnBackPressedCallback(true) {
34+
@Override
35+
public void handleOnBackPressed() {
36+
if (newFragment.onBackPressed()) {
37+
return;
38+
}
39+
finish();
40+
}
41+
});
3942
}
4043
}

0 commit comments

Comments
 (0)