50

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?

Kalimah
  • 11,217
  • 11
  • 43
  • 80

7 Answers7

57

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

Oliver Hausler
  • 4,900
  • 4
  • 35
  • 70
  • 6
    this is old but worth noting that there are two situations where this won't work. 1) when the context is the Application context (won't happen inside a view, but good to know if you're in a utility class of some kind). 2) occasionally the view context will resolve to a ContextThemeWrapper instead of an activity. I haven't worked out the cause of the later but it's something to keep in mind as it will cause a class cast exception. – Travis Castillo Feb 16 '16 at 16:58
  • Doesn't necessarily work anymore as of support library 23.3. getContext() on ImageView returns a TintContextWrapper with no possible way of retrieving the base context. – 0101100101 Apr 11 '16 at 12:32
  • @0101100101 Wrong. You can still get the basecontext by casting to ContextWrapper. – Maxr1998 May 01 '16 at 14:20
  • Sure, right, but the answer has to be updated accordingly. That's all :) – 0101100101 May 02 '16 at 13:09
52

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);
}
MByD
  • 135,866
  • 28
  • 264
  • 277
  • It did it. Thank you very much. – Kalimah Sep 11 '11 at 13:57
  • 14
    This should not be the accepted answer, because it may cause a memory leak later on if you don't clean up all manually created references. You should always use the managed context provided by Android. See my answer, instead. – Oliver Hausler Nov 11 '14 at 18:27
  • 5
    @OliverHausler - I don't see the problem , as no one talks about keeping the reference to the activity. also, you assume that the question is about calling it from a view, which is not stated there. and the last thing is that you assume that the context is the activity, which is not necessarily true. – MByD Nov 11 '14 at 18:40
  • @MByD read this: http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html - It clearly states "Do not keep long-lived references to a context-activity". – Oliver Hausler Nov 11 '14 at 18:45
  • 5
    @OliverHausler - I might be missing something, but I don't see any reference being kept here. It is being used and immediately discarded (as the method reaches its end) – MByD Nov 11 '14 at 19:25
  • MByD, even if you discard your reference immediately after use, you encourage people to pass along their own references to context, which is not the correct way of doing this on the Android platform according to @Romain Guy and Google. I can see no reason why you should be doing this if the OS provides managed access to the activity context. It produces messy code and is against best practices. – Oliver Hausler Nov 11 '14 at 19:58
24

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.

Hexise
  • 1,520
  • 15
  • 20
3

kotlin code:

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

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.

BladeCoder
  • 12,779
  • 3
  • 59
  • 51
0

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);
devio
  • 607
  • 9
  • 29
-17

Use

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

It's will be easier

wwahyudi
  • 5
  • 1