Junior — Middle
How can interface display be launched in Android without using the Activity class?
sobes.tech AI
Answer from AI
In Android, the interface is usually displayed through an Activity, but it can be done without it, for example, by using the WindowManager class to add a custom View directly to the system window. This is often used for creating overlays (such as pop-up tips or chats over other applications).
Example of adding a View without an Activity:
WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
View myView = LayoutInflater.from(context).inflate(R.layout.my_layout, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY, // or TYPE_PHONE for older versions
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT
);
windowManager.addView(myView, params);
For this, you need to have the appropriate permissions, such as SYSTEM_ALERT_WINDOW.
You can also use Dialog or PopupWindow, which do not require an Activity to display, but in any case, a context is needed.