Access onWindowFocusChanged (Activity) from Application

Hi all,

I made an app for android. From CoronaApplication.java (that extends android.app.Application) I need to access the onWindowFocusChanged (that belongs to Activity)

In detail, I have

public class CoronaApplication extends android.app.Application {

in which, after searching a lot on the net, I found the registerActivityLifecycleCallbacks

so I add, in the (override) onCreate the follwing code:

this.registerActivityLifecycleCallback(new ActivityLifecycleCallbacks(){ @Override public void onActivityCreated(Activity activity, Bundle savedInstanceState) { Log.i("TEST", "activity Started"); }

So now I can see that the activity is started.

Now I need to know when the window lose the focus.

I need to insert the following listener in the onActivityCreated:

@Override public void onWindowFocusChanged(boolean hasFocus) { if (!hasFocus) { // Method that handles loss of window focus someMethods(); } }

how have I to insert these lines???

Thanks a lot for the reply!!!

You can subclass CoronaActivity.  You will lose access to notifications and possibly other items though.  

Basically I need to override some method in the activity. But I can’t find the activity for my project!

In my AndroidManifest.XML I have:

 \<application android:name="CoronaApplication" android:label="MyApp" android:hardwareAccelerated="true" android:debuggable="true" android:icon="@drawable/ic\_launcher"\> \<!-- The main Corona activity that runs the Corona project. --\> \<activity android:name="com.ansca.corona.CoronaActivity" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation|screenSize" android:label="My App" android:launchMode="singleTask" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"\> \<intent-filter\> \<action android:name="android.intent.action.MAIN" /\> \<category android:name="android.intent.category.LAUNCHER" /\> \</intent-filter\> \</activity\>

So I can find the Application (It’s located under com.mycompany.app/COronaApplication.java) and extends android.app.Application.

But I can’t find the main activity (in which I have to override some methods). The XML says that is com.ansca.corona.CoronaActivity, but I can’t find this java file…

When, for test purpose, I created a blank app with file->new->Android Application Project, a java class that extends Activity is created, and I can work on it…

Anyone knows how can I access the Activity class for my corona app???

Thank You

You can just do:

public class MyActivity extends com.ansca.corona.CoronaActivity {

}

You don’t need the java file.

dchan, thanks for the reply

I followed you post (I think…) I made a class, FullscreenActivity.java, and put it in the same folder (com.mycompany.com) where are present AppManager.java and CoronaApplication.java.

I modifiy the constructor of this class in

public class FullscreenActivity extends com.ansca.corona.CoronaActivity

but this method

@Override public void onWindowFocusChanged (boolean hasFocus) { }

is never fired…

I also tried to add in AndroidManifest.xml the new activity

\<activity android:name="FullscreenActivity" android:theme=..... android:configChanges=...... /\>

but nothing changes

Any ideas?

Thanks

RESOLVED!! I have not to add another activity in the AndroidManifest.xml, but I have to change the main activity

from

\<activity android:name="com.ansca.corona.CoronaActivity"

 to

\<activity android:name="com.mycompany.app.FullscreenActivity"

because it extends CoronaActivity…

and follow dchan post, now it works!

Thanks!

You can subclass CoronaActivity.  You will lose access to notifications and possibly other items though.  

Basically I need to override some method in the activity. But I can’t find the activity for my project!

In my AndroidManifest.XML I have:

 \<application android:name="CoronaApplication" android:label="MyApp" android:hardwareAccelerated="true" android:debuggable="true" android:icon="@drawable/ic\_launcher"\> \<!-- The main Corona activity that runs the Corona project. --\> \<activity android:name="com.ansca.corona.CoronaActivity" android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation|screenSize" android:label="My App" android:launchMode="singleTask" android:theme="@android:style/Theme.NoTitleBar.Fullscreen"\> \<intent-filter\> \<action android:name="android.intent.action.MAIN" /\> \<category android:name="android.intent.category.LAUNCHER" /\> \</intent-filter\> \</activity\>

So I can find the Application (It’s located under com.mycompany.app/COronaApplication.java) and extends android.app.Application.

But I can’t find the main activity (in which I have to override some methods). The XML says that is com.ansca.corona.CoronaActivity, but I can’t find this java file…

When, for test purpose, I created a blank app with file->new->Android Application Project, a java class that extends Activity is created, and I can work on it…

Anyone knows how can I access the Activity class for my corona app???

Thank You

You can just do:

public class MyActivity extends com.ansca.corona.CoronaActivity {

}

You don’t need the java file.

dchan, thanks for the reply

I followed you post (I think…) I made a class, FullscreenActivity.java, and put it in the same folder (com.mycompany.com) where are present AppManager.java and CoronaApplication.java.

I modifiy the constructor of this class in

public class FullscreenActivity extends com.ansca.corona.CoronaActivity

but this method

@Override public void onWindowFocusChanged (boolean hasFocus) { }

is never fired…

I also tried to add in AndroidManifest.xml the new activity

\<activity android:name="FullscreenActivity" android:theme=..... android:configChanges=...... /\>

but nothing changes

Any ideas?

Thanks

RESOLVED!! I have not to add another activity in the AndroidManifest.xml, but I have to change the main activity

from

\<activity android:name="com.ansca.corona.CoronaActivity"

 to

\<activity android:name="com.mycompany.app.FullscreenActivity"

because it extends CoronaActivity…

and follow dchan post, now it works!

Thanks!