0

Ok, I'm going to try to explain this one as easy as possible! I'm sure I'm overlooking something simple - but have been staring at the screen too long to identify. As always, thanks for helping!

In my Main activity I have:

Sub Activity_KeyPress (KeyCode As Int) As Boolean 'handles the back key and the menu key
    If KeyCode = KeyCodes.KEYCODE_BACK Then 'back keySub Activity_KeyPress (KeyCode As Int) As Boolean                          
        Quitandsave
    End If
    Return True
End Sub

The Sub - Quitandsave adds a panel view to the current activity (in Main) - on the panel are two buttons - yes to quit and no (change their mind and return to the application).

What I'm finding is that when the back key is pressed - the panel is displayed properly, and the yes/no buttons work fine.

However, if while the panel is visible from the initial back key press, and then the back key is pressed once again (on purpose or accident) - the yes button still works (panel removes, files saves, app quits), but the no button doesn't - like it is frozen - and the panel won't remove. Is it because of initialize? ... or something else?

Here is my yes/no button code:

Sub quitsavebtn1_Click ' yes - quit and save
    WriteMapSavePage 'calls a sub that writes map to int or external space
    Activity.Finish ' exit the application
End Sub

Sub quitsavebtn2_Click 'no - don't quit and save
    quitsavepnl.RemoveView 'remove the panel to continue using app
End Sub

In my Quitandsave Sub:

Sub Quitandsave
    quitsavepnl.Initialize("quitsavepnl")
    .... code here to set colors,font etc.
    Activity.AddView(quitsavepnl,15dip,15dip, 50%x, 50%y)
    .... code here to add buttons to panel etc.
    quitsavepnl.Visible = True
End Sub
Tony Moreira
  • 93
  • 3
  • 18

3 Answers3

2

RemoveView doesn't do what you seem to think. It just detaches the Panel from it's parent. Using a Msgbox would be better but you could try quitsavepnl.Visible = False instead.

agraham
  • 101
  • 2
  • I originally had msgbox, & then dialogue library - with the msgbox I wanted a custom look. With the dialogue library I used no built in buttons - created some - but I think I read at B4A thread - you can't capture click event to perform outside the dialogue. Anyhow - using Visible=False here as you suggested gives me the same result - if I hit the back key while this panel is visible (a user may do by accident) the no button doesn't do anything & the panel remains - the yes button works fine (file saves/app ends). If I don't hit the back key while the panel is visible - both buttons work ok. – Tony Moreira Oct 23 '11 at 15:37
  • "you can't capture click event to perform outside the dialogue" You can. The CustomDialogs demo shows this for a Button. – agraham Oct 25 '11 at 09:28
1

Try removing this line, and see if it works:

quitsavepnl.RemoveView

You may need a Timer to remove the view after the click event is finished.

Erel
  • 1,802
  • 2
  • 15
  • 58
  • Is it still frozen? Use Log to check whether the event is raised or not. – Erel Oct 23 '11 at 15:42
  • That is correct - when using visible=false the no button does not respond at all - I don't see activity in the log. The yes button gives me: Activity Main UserClosed = true. When I use quitsavepnl.RemoveView the no button wokrs (panel goes away) - no activity seen in log. And then the original problem still exists - an additional back key while panel is on screen renders no button unresponsive. – Tony Moreira Oct 23 '11 at 16:01
  • You will need to post your complete project (File - Export As zip). You can upload it to the forum. – Erel Oct 23 '11 at 16:09
  • ok - i'll see what i can do - lot's of stuff to remove as it is a customer project. – Tony Moreira Oct 23 '11 at 16:15
0

Try changing the function quitsavebtn2_Click to the following:

Sub quitsavebtn2_Click ' no - don't quit and save
    If quitsavepnl.IsInitialized then
        quitsavepnl.Visible = false ' remove the panel to continue using app

    End if

End Sub

...and the function QuitandSave to the following:

Sub QuitandSave
    If Not(quitsavepnl.IsInitialized) then
        quitsavepnl.Initialize("quitsavepnl")
        ' .... code here to set colors,font etc.

        Activity.AddView(quitsavepnl, 15dip, 15dip, 50%x, 50%y)
        ' .... code here to add buttons to panel etc.

    End If

    If quitsavepnl.IsInitialized then
        quitsavepnl.Visible = True

    End If

End Sub

What makes the code above so effiecient is that it works with Java:

  • The Panel will persist until Java's garbage collector deletes the Panel to free memory.
  • The Panel will then be recreated the next time QuitandSave is called.
WonderWorker
  • 8,539
  • 4
  • 63
  • 74