P.Zaccaria
P.Zaccaria

Reputation: 311

Calling a non-static method from outside the class

I often have to deal with this kind an error when programming in Java on Android. For example I have a class where I set a flag.

public class ViewActivity extends Activity {  
...

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    ...
}

In another class I want to reset the FLAG_KEEP_SCREEN_ON

class DrawOnTop extends View {
...
if (condition) {
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

but this doesn't work, since I get "The method getWindow is undefined for the type DrawOnTop".

So I try to define a clearFlags method in ViewActivity class

void clearFlags() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

and to call it from the DrawOnTop class:

    if (condition) {
        ViewActivity.clearFlags();
    }

This doesn't work as well: I get "Cannot make a static reference to the non-static method clearFlags() from the type ViewActivity". Well, let's make it static then.

static void clearFlags() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

and then I get "Cannot make a static reference to the non-static method getWindow from the type Activity"

How could I execute such a statement?

Upvotes: 4

Views: 1272

Answers (4)

P.Zaccaria
P.Zaccaria

Reputation: 311

I tried to implement the solutions suggested by Aksaçlı and this turned out to be very simple:

In the ViewActivity class DrawonTop is called this way: mDrawOnTop = new DrawOnTop(this);

The constructor of the second class contains this:

public DrawOnTop(Context context) {
            super(context);

Therefore ViewActivity.clearFlags(); has simply to be rewritten as ((ViewActivity)getContext()).clearFlags();

Upvotes: 0

Sedat Polat
Sedat Polat

Reputation: 1721

You can send getWindow() as parameter into clearFlags method. Call clearFlags(Window window) from your activity: WindowHelper.getInstance().clearFlags(getWindow());

Helper class:

public class WindowHelper {

    public static final WindowHelper instance = new WindowHelper();

    public static WindowHelper getInstance() {
        return instance;
    }

    public void clearFlags(Window window) {
        window.clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    }
}

Upvotes: 0

Aksaçlı
Aksaçlı

Reputation: 86

If your DrawOnTop class is nested within the ViewActivity you can create a local Context variable and use it to call the getWindow(). If that's not the case then create a receiver in your activity class then from DawOnTop send an intent with your trigger to do whatever the job is. Do not instantiate your activity class, bad idea!

Upvotes: 2

Titulum
Titulum

Reputation: 11456

Perhaps you should refer to an initialised object in your static method. So instead of:

void clearFlags() {
    getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

you should create a static instance variable of your window:

private static staticWindowInstance;

void clearFlags() {
getStaticWindowInstance().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}

For more information, you should check out the Singleton design pattern.

Upvotes: -1

Related Questions