Moving an application to foreground / to background
23 Jul 2003 - 16:24
Keywords :

This article will show you how you can control the behaviour of your application so that it knows when it gains or looses the screen focus and how to control it.

Being notified of the focus change

The Series 60 framework will notify an application when it gains or looses the screen focus through a call to CAknAppUi::HandleForegroundEventL(TBool aForeground). The aForeground parameter equals ETrue when your app gains the screen focus, and EFalse when it looses it.

If you need to do some specific stuff, you should override this function (and call the base class implementation). Here is an example for an application that don't want to loose the focus:

void CMyAppUi::HandleForegroundEventL(TBool aForeground)
{
 // Call Base class method
 CAknAppUi::HandleForegroundEventL(aForeground);

 if(aForeground)
 {
     // We have gained the focus
     ...
 }
 else
 {
     // We have lost the focus
     ...
 }
}

Changing the focus

You can also request to change the focus of your application. The commands are TApaTask::SendToBackground() and TApaTask::BringToForeground().

Here is how you can use them (from the AppUi):

void CMyAppUi::BringToForeground()
{
 // Construct en empty TApaTask object
 // giving it a reference to the Window Server session
 TApaTask task(iEikonEnv->WsSession( ));
               
 // Initialise the object with the window group id of
 // our application (so that it represent our app)
 task.SetWgId(CEikonEnv::Static()->RootWin().Identifier());

 // Request window server to bring our application
 // to foreground
 task.BringToForeground();
}

I never tested the following code, but you may be able to control the focus of other applications by using:

 // Bring the application "theApp" to background
 TApaTaskList tasklist(iCoeEnv->WsSession());
 TApaTask     task(tasklist.FindApp(_L("theApp")));
 task.SendToBackground();  // or BringToForeground()

Don't forget to link against apgrfx.lib
Tutorial posted July 23rd, 2003 by eric

Submitted by Anonymous on Thu, 2003-07-24 15:04.

Just as a note..... the last code snippet for sending other apps to the foreground works.

Submitted by Anonymous on Tue, 2003-09-09 08:06.

But this code does'nt work during incoming call (Phone app hides other windows).

Submitted by eric (not verified) on Tue, 2003-09-09 11:05.

Yes... The phone application has a very high priority. So you have to be more creative to achieve this!

Submitted by Anonymous on Fri, 2003-10-03 07:50.

So what say you, is it even possible to override phone priority?

Truly hope so...

thx, pzn


Submitted by Anonymous on Fri, 2004-03-12 20:18.

I have a server that listens to incoming voice calls and displays a global confirmation dialog when a call is received. The confirmation dialog is always displayed "beneath" the phone app which comes to foreground.

Is there any way of bringing my dialog to foreground or taking the phone app to background?


Submitted by Anonymous on Thu, 2004-04-01 05:33.

No, phone app, bluetooth, SMS message received and other soft notes use the Notification framework (RNotifier) of Symbian which has priority of KMaxInt so you can't have higher priority than them.

Submitted by kundutech (not verified) on Mon, 2005-02-21 11:13.

Try moving an application to foreground / to background when it's property in CApaWindowGroupName is hidden, e.g. it's not shown in the tasklist. Then TApaTaskList will never find it.
You migth be better of using for bringin the app to foreground:
RWsSession ws;
User::LeaveIfError(ws.Connect());
ws.SetWindowGroupOrdinalPosition(iWindowsGroupID, 0);
ws.Close();

iWindowsGroupID is a member variable where is store my WindowsGroupId just before loosing focus/being sent to background.
To send it to background:
RWsSession ws;
User::LeaveIfError(ws.Connect());
iWindowsGroupID= ws.GetFocusWindowGroup();
ws.SetWindowGroupOrdinalPosition(iWindowsGroupID, -1);
ws.Close();


Submitted by shravan_vip on Mon, 2008-03-17 10:49.

Please can u explain me in brief about ur code. I want to know what should be the iWindowsGroupId. whether we should give any value for it.

Or can u help me in writing a code for moving any of our nokia applications to foreground from another application. Please Its urgent.


Submitted by mayur on Tue, 2008-03-25 06:29.

hiiii,
acctually i m also suffering with the same problem. if u find then help me.


Submitted by Lucian (not verified) on Fri, 2005-04-01 11:38.

Hello.

Your code worked.

One question thought.... How do i listen for system events for example? More exactly how do i measure system idle periods? (remember that my application runs in the background...)

So, what i want to do is make my application pop-up (run in foreground) after 10sec of inactivity from the user with the phone.

How do i do this?

Lucian


Submitted by miguel (not verified) on Sun, 2005-04-03 10:10.

Hi Eric, I have some problems with this script.Can you explain me how put this script in my code,please?For example where I have instance the variable "iEikonEnv", where I have put the second script, in which file? I'm new in Symbian's environment. thanks.

Submitted by eric on Thu, 2005-04-07 08:29.

You can use CEikonEnv::Static() instead of iEikonEnv

Submitted by Anonymous on Tue, 2005-04-26 19:36.

Has anybodu got success in setting application forground during incoming call

Let me know i need it very urgently.


Submitted by Hugo Maurício (not verified) on Thu, 2005-08-18 17:33.

Hi all!!

I can't connect to the RWsSession. I get the following error

error LNK2019: unresolved external symbol "public: int __thiscall RWsSession::Connect(void)"

I tried both Eric's and kundutech's sugestions

Help please!!

Many Thanks!!!!! :) :)


Submitted by eric on Thu, 2005-08-18 17:46.

Edit your MMP file so that your app links against the library that contains the missing class.


copyright 2003-2009 NewLC SARL