Kalimah
Kalimah

Reputation: 11437

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 inside an Activity?

Upvotes: 50

Views: 120829

Answers (7)

BladeCoder
BladeCoder

Reputation: 12929

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.

Upvotes: 1

devio
devio

Reputation: 654

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);

Upvotes: 0

jsaon chyeng
jsaon chyeng

Reputation: 31

kotlin code:

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

Upvotes: 3

Hexise
Hexise

Reputation: 1568

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.

Upvotes: 25

Oliver Hausler
Oliver Hausler

Reputation: 4977

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().

Upvotes: 57

wwahyudi
wwahyudi

Reputation: 5

Use

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

It's will be easier

Upvotes: -17

MByD
MByD

Reputation: 137272

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);
}

Upvotes: 52

Related Questions