I have an Player activity and Floating Player (service), after click on back button the Player Activity is close and the Floating Player is goes into action, when I click on the Floating Player I want to back to the Player Activity but with no run again the onCreate method.
I read a lot of solution in StackOver Flow, especially use the FLAG_ACTIVITY_CLEAR_TOP and FLAG_ACTIVITY_SINGLE_TOP flags, or add android:launchMode="singleTask" / android:launchMode="singleInstance" to the Player Activity in the Manifest, but none of them not really work.
I would be happy if there is a real solution that make a single task to my Player Activity, and not reopen it from onCreate method.
Player Activity:
public class MainActivity extends AppCompatActivity {
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("https://www.youtube.com/embed/bWsIJ7UVxQs");
Toast.makeText(this, "onCreate is running!", Toast.LENGTH_SHORT).show();
}
@Override
public void onBackPressed() {
super.onBackPressed();
startService(new Intent(MainActivity.this, WindowPlayer.class));
}
}
Floating player (service):
public class WindowPlayer extends Service {
private WindowManager windowManager;
private View contFloatingPlayer;
private WindowManager.LayoutParams contPlayerParams;
private WebView webView;
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@SuppressLint("ClickableViewAccessibility")
@Override
public void onCreate() {
super.onCreate();
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
initFloatingPlayer();
webView.loadUrl("https://www.youtube.com/embed/bWsIJ7UVxQs");
webView.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
contFloatingPlayer.setVisibility(View.GONE);
webView.destroy();
return false;
}
});
webView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction() & MotionEvent.ACTION_MASK) {
case MotionEvent.ACTION_UP:
Intent backToStablePlayer = new Intent(getApplicationContext(), MainActivity.class);
backToStablePlayer.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
Intent.FLAG_ACTIVITY_CLEAR_TOP |
Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(backToStablePlayer);
}
return false;
}
});
}
@SuppressLint({"ClickableViewAccessibility", "SetJavaScriptEnabled", "AddJavascriptInterface"})
private void initFloatingPlayer() {
int LAYOUT_FLAG;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_FLAG = WindowManager.LayoutParams.TYPE_PHONE;
}
contPlayerParams = new WindowManager.LayoutParams(
532,
300,
LAYOUT_FLAG,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
contPlayerParams.gravity = Gravity.TOP | Gravity.LEFT | Gravity.START;
contPlayerParams.x = 0;
contPlayerParams.y = 100;
contFloatingPlayer = LayoutInflater.from(this).inflate(R.layout.activity_main, null, false);
windowManager.addView(contFloatingPlayer, contPlayerParams);
webView = contFloatingPlayer.findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
}
Note: I added Intent.FLAG_ACTIVITY_NEW_TASK to the intent because it's intent from service, else it's crash.