In this appui I am trying to make my application run foreground and background both . While I can go background , while trying to come forground screen , object whose deletion was called gets deleted but is not getting refreshed to foreground. I am getting stuck at an empty application screen. Please help.
Code:
#include "AknExGridAppUi.h"
#include <f32file.h>
#include <eikenv.h>
#include <eikapp.h>

#include <eikmenub.h>
#include <eikmenup.h>
#include "Aknexgrid.hrh"
#include "Aknexgrid.hlp.hrh"
#include "LoginForm.h"

void CAknExGridAppUi::ConstructL()
    {
    ......       }

CAknExGridAppUi::~CAknExGridAppUi()
    {
    }

TBool CAknExGridAppUi::KeyCapturedL(TWsEvent /*aEvent*/)
	{
	return ETrue;
	}

void CAknExGridAppUi::ForeGround()
	{
	iDrawer0->DoCancel();
	delete iDrawer0 ;
	iDrawer0=NULL;
	iDrawer1->DoCancel();
	delete iDrawer1 ;
	iDrawer1=NULL;
	iCapture->DoCancel();
	iCapture->Cancel();
//	iCapture->iActive=EFalse;
	delete iCapture ;
	iCapture=NULL;
	   iBackground=EFalse;
	TInt iWindowsGroupID;
	RWsSession ws;
	User::LeaveIfError(ws.Connect());//
	iWindowsGroupID = CEikonEnv::Static()->WsSession().GetFocusWindowGroup();
	ws.SetWindowGroupOrdinalPosition(iWindowsGroupID, 0);
	ws.Close();
	}


	void CAknExGridAppUi::BackGround()
	{
	iBackground=ETrue;
	TInt iWindowsGroupID;
	RWsSession ws;
	User::LeaveIfError(ws.Connect());
	iWindowsGroupID = ws.GetFocusWindowGroup();
	ws.SetWindowGroupOrdinalPosition(iWindowsGroupID, -3);
	ws.Close();
	}


void CAknExGridAppUi::HandleCommandL( TInt aCommand )
    {
    switch ( aCommand )
        {
        case EEikBidCancel:
       {
       if(iLogin)
    	   { Exit();
    	   iLogin=NULL;
    	   }
       else
    	   {
    	   if(iDesktopTicker)
           	{

           	}	
           else
        	   { iGridView->ShowGrid();
        	   		
        	   }
       }
       }
        break;
        case EAknExGridCmdSelection:

        iGridView->iGrid->ProcessCommandL(aCommand );
         break;
        case EEikCmdExit:
        case EAknCmdExit:
            {
            if(iDesktopTicker)
               	{

               	BackGround();
               	iDrawer0 =new (ELeave) CDrawer();
               	iDrawer0->SetMopParent( this );
               	iDrawer0->ConstructL( TRect(TPoint(0,0), TSize(ClientRect().Width(),16))
               			,0 ,KRgbDarkBlue,KRgbWhite,23);
               	iDrawer1 =new (ELeave) CDrawer();
               	iDrawer1->SetMopParent( this );
               	iDrawer1->ConstructL(  TRect(TPoint(0,16), TSize(ClientRect().Width(),16))
               			,1,KRgbYellow, KRgbDarkBlue,33);
               	iCapture= CKeyCapture::NewL(*this);
               	iCapture->ConstructL(/*iWsSession,iwindowID*/);
               	}	
               else
            	   { if(iBackground)
        	   {}
           else
            Exit();}
//            HandleCommandL(EEikBidCancel );
            break;
            }
        case EAknExGridCmdClose:
        	{
             ShowGrid();
             iGridView->ShowGrid();
        	}
        break;
        case EAknExGridCmdCloseTicker:
        iDesktopTicker=EFalse;
        break;
        case EAknExGridCmdTicker:
        iDesktopTicker=ETrue;
        break;
        case EAknExGridCmdHelp:
        	{CArrayFix<TCoeHelpContext>* buf = CCoeAppUi::AppHelpContextL();
        	HlpLauncher::LaunchHelpApplicationL(iEikonEnv->WsSession(), buf);}
            break;
        default:
            break;
        }
    }

void CAknExGridAppUi::DynInitMenuPaneL(TInt aResourceId,CEikMenuPane* aMenuPane)
	{
	if(aResourceId==R_EXGR_MENUPANE_MAIN)
	{
    if(iDesktopTicker)
    	{ aMenuPane->SetItemDimmed(EAknExGridCmdTicker,ETrue);	
    	  aMenuPane->SetItemDimmed(EAknExGridCmdCloseTicker,EFalse); 
    	}    	
    else
    	{
    	aMenuPane->SetItemDimmed(EAknExGridCmdTicker,EFalse);	
        aMenuPane->SetItemDimmed(EAknExGridCmdCloseTicker,ETrue);
        }
	}
	}
void CAknExGridAppUi::StartSplashScreen(const TRect &bRect)
	{   
	    
	    //	    iEikonEnv->AppUiFactory()->MenuBar()
		iSplash = new (ELeave) CSplashScreen;
		iSplash->SetMopParent( this );
		iSplash->ConstructL(bRect,this );
		AddToStackL( iSplash );
	    //	    iSplash->SetMopParent(iSplashView);
		/*start the timer*/
		iSplashTimer = CSplashTimer::NewL(EPriorityNormal, *this);
		CleanupStack::PushL(iSplashTimer);
		iSplashTimer->StartTimer(3000000);
		CleanupStack::Pop( iSplashTimer );
		iSplash->DrawNow();
		iSplash->MakeVisible(true);
	}
//Current
void CAknExGridAppUi::TimerExpired(TAny* /*aTimer*/, TInt /*aError*/)
	{
		iSplashTimer->StopTimer();
//       iLogin=
		RemoveFromStack(iSplash);
		if(iSplashTimer != (CSplashTimer *)NULL)
			{
				delete iSplashTimer;
				iSplashTimer = (CSplashTimer *)NULL;
			}
			if(iSplash != (CSplashScreen *)NULL)
				{
					delete iSplash;
					iSplash = (CSplashScreen *)NULL;
				}		

     iLogin=CLogin::NewL();
    if(iLogin->ExecuteLD(R_LOGINFORM_FORM_DIALOG))
	{
			ShowGrid();
			iLogin=NULL;

	}
			
		if(iGridView != (CAknExGridView *)NULL)
			{			
			iGridView->MakeVisible(ETrue);
//			iGridView->ActivateL();
			}
		if(!iShowingTicker)
	   		FetchTicker();
	   		iShowingTicker=ETrue;
	   		iShowingList=EFalse;     
	}
void CAknExGridAppUi::ShowGrid()
{
//BaseConstructL();
if(iGridView==(CAknExGridView*)NULL)
{iGridView = CAknExGridView::NewL(this);
// Cba()->MakeVisible(ETrue);
//StatusPane()->MakeVisible(EFalse);
iGridView->SetMopParent( this );
iGridView->ConstructL( ClientRect(),EAknExGridSelectionGrid, ApplicationRect() );
AddToStackL( iGridView );

}
//RWindowGroup(iEikonEnv->WsSession()).EnableReceiptOfFocus(ETrue);
if(!iShowingTicker)
		FetchTicker();
		iShowingTicker=ETrue;
		iShowingList=EFalse;
if(iBackground)
			ForeGround();		
//iGridView->ShowGrid();
}
void CAknExGridAppUi::FetchTicker()
	{
	_LIT(KFetchTicker,"Fetching Ticker");
	iLog.Write(KFetchTicker);
//////////////Set transaction observer
	if(iEngine==NULL)
		{iEngine = CPMHttpEngine::NewL(this);
		iEngine->SetTransactionObserver(iGridView->idatapuller);}	
       iEngine->FetchTickerL();
	}
void CAknExGridAppUi::FetchStock()
	{
	iGridView->ShowList();
	_LIT(KFetchStock,"Fetching Stock");
	iLog.Write(KFetchStock);
//	if(iEngine==NULL)
//	{iEngine = CPMHttpEngine::NewL(this);
//		iEngine->SetTransactionObserver(iGridView->idatapuller);
//		}
	iEngine->FetchStocksL();
	}

TKeyResponse CAknExGridAppUi::HandleKeyEventL( const TKeyEvent& aKeyEvent, TEventCode aType )
	{
	TKeyResponse retval=EKeyWasNotConsumed;
  	if(aKeyEvent.iScanCode==167&&aType==EEventKeyDown)
		{HandleCommandL( EAknExGridCmdSelection );
		retval=EKeyWasConsumed;
		}
	else
		{	if(iGridView)
	 iGridView->OfferKeyEventL(aKeyEvent,aType );
	 retval=EKeyWasConsumed;}
	return retval;
	}
void CAknExGridAppUi::HandleControlEventL(
    CCoeControl* /*aControl*/, TCoeEvent /*aEventType*/ )
    {
    // No implementation required
    }
The forground() is being called from
Code:
void CKeyCapture::RunL()
{
	if (iStatus == KErrNone) 
	{
		TWsEvent e;
		iWsSession.GetEvent(e);
		TInt type = e.Type();
 
		switch (type)
		{
		
		case EEventKey:	
		{
		  if(iObserver.KeyCapturedL(e))
			  {TInt code=e.Key()->iScanCode;
			  if(e.Key()->iScanCode==164)
				{
/**********************function called from here *************/
CEikonEnv::Static()->EikAppUi()->HandleCommandL(EAknExGridCmdClose);
/************************************************************/
				iamActive=EFalse;
				}
			}
//			    TInt wgId = iWsSession.GetFocusWindowGroup();
////				TInt wgId = CEikonEnv::Static()->RootWin().Identifier();//iWsSession.GetFocusWindowGroup();
//				iWsSession.SendEventToWindowGroup(wgId, e);
			   }
			break;
		case EEventKeyUp:
		break;
		case EEventKeyDown:
		if(iObserver.KeyCapturedL(e))
		{if(e.Key()->iScanCode==167)
			{CEikonEnv::Static()->EikAppUi()->HandleCommandL(EAknExGridCmdClose);
			iamActive=EFalse;}
//			iStatus= KErrCancel;
		}
			break;
		};
	}
	
	if ((iStatus != KErrCancel)&&iamActive) 
	{
		Listen();
	}
	
}