i try to go from one fragment to another within the floating window using buttons. I manage the fragments using a activity that contains them but when using the button on the first fragment (Ubicacion1Fragment.java) it displays a blank view with nothing on it and it is imposible to go back using the back button buit on the phone.
I tried using the logs to see the problem but i am not able to see any of the logs i wrote on the logcat.
The floating window is setup as a service FloatingWindowService.java and i have a class that manages the service FloatingWindowGFG.java this service and the class that manages the services i copied from this web https://www.geeksforgeeks.org/how-to-make-a-floating-window-application-in-android/ and im trying to change it to my needs.
Sorry if i explin it wrong english is not my first lenguage.
FloatingWindowService.java
package com.tfg.ayuda;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.provider.Settings;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;
public class FloatingWindowService extends Service {
private WindowManager windowManager;
private View popupView;
@SuppressLint("InflateParams")
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onCreate() {
super.onCreate();
Log.d("FloatingWindowService", "Service created");
if (!Settings.canDrawOverlays(this)) {
Log.e("FloatingWindowService", "Overlay permission not granted");
stopSelf();
return;
}
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
popupView = inflater.inflate(R.layout.activity_whatsapp, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
params.gravity = Gravity.CENTER;
params.x = 0;
params.y = 0;
windowManager.addView(popupView, params);
Log.d("FloatingWindowService", "Popup view added to window manager");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.d("FloatingWindowService", "Service started");
return START_STICKY;
}
@Override
public void onDestroy() {
super.onDestroy();
if (popupView != null) {
windowManager.removeView(popupView);
popupView = null;
}
Log.d("FloatingWindowService", "Service destroyed and view removed");
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
FloatingWindowGFG.java
package com.tfg.ayuda;
import android.annotation.SuppressLint;
import android.app.Service;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Build;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.Button;
import androidx.annotation.Nullable;
public class FloatingWindowGFG extends Service {
private ViewGroup floatView;
private WindowManager.LayoutParams floatWindowLayoutParam;
private WindowManager windowManager;
private String fragmentType; // Para saber qué fragmento inflar
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
@SuppressLint("InflateParams")
@Override
public void onCreate() {
super.onCreate();
// Calcular el tamaño de la pantalla
DisplayMetrics metrics = getApplicationContext().getResources().getDisplayMetrics();
int width = metrics.widthPixels;
int height = metrics.heightPixels;
windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
LayoutInflater inflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
// Inflar el diseño basado en el fragmento
if ("UBICACION1".equals(fragmentType)) {
floatView = (ViewGroup) inflater.inflate(R.layout.fragment_ubicacion1, null);
} else if ("UBICACION2".equals(fragmentType)) {
floatView = (ViewGroup) inflater.inflate(R.layout.fragment_ubicacion2, null);
} else {
// Manejar el caso cuando el tipo de fragmento es desconocido
floatView = (ViewGroup) inflater.inflate(R.layout.fragment_ubicacion1, null); // Predeterminado
}
Button maximizeBtn = floatView.findViewById(R.id.siguienteUbicacion1);
int LAYOUT_TYPE;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
LAYOUT_TYPE = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
LAYOUT_TYPE = WindowManager.LayoutParams.TYPE_TOAST;
}
floatWindowLayoutParam = new WindowManager.LayoutParams(
(int) (width * (0.80f)),
(int) (height * (0.30f)),
LAYOUT_TYPE,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
floatWindowLayoutParam.gravity = Gravity.CENTER;
floatWindowLayoutParam.x = 0;
floatWindowLayoutParam.y = 0;
windowManager.addView(floatView, floatWindowLayoutParam);
maximizeBtn.setOnClickListener(v -> {
stopSelf();
windowManager.removeView(floatView);
Intent activityIntent = new Intent(FloatingWindowGFG.this, FragmentContainerActivity.class);
activityIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(activityIntent);
});
floatView.setOnTouchListener(new View.OnTouchListener() {
final WindowManager.LayoutParams floatWindowLayoutUpdateParam = floatWindowLayoutParam;
double x;
double y;
double px;
double py;
@SuppressLint("ClickableViewAccessibility")
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
x = floatWindowLayoutUpdateParam.x;
y = floatWindowLayoutUpdateParam.y;
px = event.getRawX();
py = event.getRawY();
break;
case MotionEvent.ACTION_MOVE:
floatWindowLayoutUpdateParam.x = (int) ((x + event.getRawX()) - px);
floatWindowLayoutUpdateParam.y = (int) ((y + event.getRawY()) - py);
windowManager.updateViewLayout(floatView, floatWindowLayoutUpdateParam);
break;
}
return false;
}
});
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// Obtener el tipo de fragmento desde el Intent
if (intent != null) {
fragmentType = intent.getStringExtra("FRAGMENT_TYPE");
}
// Continuar con la creación de la ventana flotante
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
super.onDestroy();
stopSelf();
if (floatView != null) {
windowManager.removeView(floatView);
}
}
}
Ubicacion1Fragment.java
package com.tfg.ayuda;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import android.util.Log;
public class Ubicacion1Fragment extends Fragment {
public Ubicacion1Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ubicacion1, container, false);
Button buttonToSecondFragment = view.findViewById(R.id.siguienteUbicacion1);
Log.d("Ubicacion1Fragment", "onCreateView: Button to second fragment found.");
buttonToSecondFragment.setOnClickListener(v -> {
FragmentManager fragmentManager = getParentFragmentManager(); // Use getParentFragmentManager instead of getChildFragmentManager
Ubicacion2Fragment ubicacion2Fragment = new Ubicacion2Fragment();
Log.d("Ubicacion1Fragment", "onCreateView: Navigating to Ubicacion2Fragment");
fragmentManager.beginTransaction()
.replace(R.id.fragment_container, ubicacion2Fragment)
.addToBackStack(null)
.commit();
});
Log.d("Ubicacion1Fragment", "onCreateView: Ubicacion1Fragment created successfully.");
return view;
}
}
Ubicacion2Fragment.java
package com.tfg.ayuda;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.fragment.app.Fragment;
public class Ubicacion2Fragment extends Fragment {
public Ubicacion2Fragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_ubicacion2, container, false);
return view;
}
}
i tried looking for an error there was none, then the logs and i could not find them on the logcat, then i tried to see if there was any spelling mistake or if the id where duplicate but no and i dont know what else to look for