开发者

How to call getWindow() outside an Activity in Android?

I am trying to organize my code and move repetitive functions to a single class. This line of code works fine inside a class that extends activity:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

However it is not working when I try to include it into an external class.

How do I call getWindow() from another class to apply it in开发者_如何学Goside an Activity?


You shall not keep references around as suggested in the accepted answer. This works, but may cause memory leaks.

Use this instead from your view:

((Activity) getContext()).getWindow()...

You have a managed reference to your activity in your view, which you can retrieve using getContext(). Cast it to Activity and use any methods from the activity, such as getWindow().


Pass a reference of the activity when you create the class, and when calling relevant methods and use it.

void someMethodThatUsesActivity(Activity myActivityReference) {
    myActivityReference.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
}


You can use following method to cast current context to activity:

/**
 * Get activity instance from desired context.
 */
public static Activity getActivity(Context context) {
    if (context == null) return null;
    if (context instanceof Activity) return (Activity) context;
    if (context instanceof ContextWrapper) return getActivity(((ContextWrapper)context).getBaseContext());
    return null;
}

Then you can get window from the activity.


kotlin code:

myView.rootView.findViewById<View>(android.R.id.content).context as Activity


A Window instance must always be retrieved from either an Activity or a Dialog. So the proper way is to pass the Window reference to your component, for example through dependency injection, then make sure your component does not outlive the Activity or Dialog in order to avoid memory leaks.

Note that is incorrect to assume you can always retrieve the correct Window by looking for the Activity starting from a View's Context, because sometimes that View is inside a Dialog which uses a different Window.


to call getWindow() outside an Activity in Android you need to pass the activity object in this method i set the parameter Acticity for example this activity set full screen to an activity

 public void fullScreen(Activity activity){
    activity.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    activity.getWindow().getDecorView().setSystemUiVisibility(
            View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}

then when you use it set MainActivity.this argument

yourClassInstance.fullScreen(MainActivity.this);


Use

getActivity().getWindow().requestFeature(Window.FEATURE_PROGRESS);

It's will be easier

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜