Moving an application to foreground / to background

Platforms:
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

> Moving an application to foreground / to background

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

> Moving an application to foreground / to background

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

> Moving an application to foreground / to background

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

> Moving an application to foreground / to background

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

Truly hope so...

thx, pzn

> Moving an application to foreground / to background

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?

> Moving an application to foreground / to background

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.

> Moving an application to foreground / to background

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

Re: > Moving an application to foreground / to background

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.

Re: > Moving an application to foreground / to background

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

> Moving an application to foreground / to background

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

> Moving an application to foreground / to background

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.

> Moving an application to foreground / to background

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

> Moving an application to foreground / to background

Has anybodu got success in setting application forground during incoming call

Let me know i need it very urgently.

> Moving an application to foreground / to background

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!!!!! :) :)

> Moving an application to foreground / to background

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