Android Question App crashes when button back is pressed

ivavilagu

Member
Licensed User
Longtime User
I have the next code:
B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean 'return true if you want to consume the event
    Dim result As Int
    If KeyCode = KeyCodes.KEYCODE_BACK Then
          result=  Msgbox2("Do you want to exit?", "", "Yes", "", "No", Null)
        If result = DialogResponse.POSITIVE Then
          Activity.Finish
        Else
            Return True
        End If
    End If
End Sub

The app works fine in the emulator but when I try in a phone (moto G) or in a tablet (nexus 7) the app crashes in both cases (pressing 'yes' or pressing 'no').

The log window doesn´t show anything
 

nwhitfield

Active Member
Licensed User
Longtime User
Is this related to that change in handling of the Back key in some recent versions of Android? I recall Erel posted a workaround some time back. So, what I have in my app now is something like this:

(the bit about visible panels is so that if certain things are on screen - in this case panels for entering new info - they just get hidden, and the main display shown instead). backcounter is a global int, set to 0 whenever you want to ensure that two presses are needed to exit the app, which I use instead of a Yes/No dialog

I also check to see if the app's registered for notifications, and turn them off; in my GCM service, I then call a sub back in Main when the unregistration is done, called 'bye_bye', which does an Activity.finish for me.

B4X:
Sub Activity_KeyPress( KeyCode As Int) As Boolean
   ' trap the back key
   If KeyCode = KeyCodes.KEYCODE_BACK Then
  
     CallSubDelayed(Me,"backhandler")
     Return True
    
   Else
     Return False
   End If
End Sub

Sub backhandler
   ' return to the main view from the composer
   If editPanel.Visible = True OR taskPanel.Visible = True Then
     editPanel.Visible = False
     taskPanel.Visible = False
     blViewer.Visible = True
   Else
     If backCounter = 0 Then
       ToastMessageShow("Press Back again to exit",False)
       backCounter = 1
     Else
       If GCMregistered Then
         CallSubDelayed2(PushService,"RegisterDevice",True)
       Else
         Activity.Finish
       End If
     End If
   End If
End Sub
 
Upvote 0

ivavilagu

Member
Licensed User
Longtime User
Maybe it´s for software version. Both devices have the 4.4.4 android version and the emulator has 4.2.2

Your code works well ;) but you need to add a timer to reset the backcounter variable after passing 3 o 4 seconds. If you press for second time the back button having spent two minutes the app will exit without showing the message.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi ivavilagu,

maybe the problem in your code lays in a missing "Return False" before the Activity.Finish.
Anyway, since the back key causes the Activity to enter its Pause sub with UserClosed=True, why you feel the need to call Activity.Finish there?
The following should do exactly the same:
B4X:
Sub Activity_KeyPress (KeyCode As Int) AsBoolean 'return true if you want to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
  If Msgbox2("Do you want to exit?", "", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then
    Return False
  Else
    Return True
  End If
End If
End Sub

Umberto
 
Upvote 0

ivavilagu

Member
Licensed User
Longtime User
Hi ivavilagu,

maybe the problem in your code lays in a missing "Return False" before the Activity.Finish.
Anyway, since the back key causes the Activity to enter its Pause sub with UserClosed=True, why you feel the need to call Activity.Finish there?
The following should do exactly the same:
B4X:
Sub Activity_KeyPress (KeyCode As Int) AsBoolean 'return true if you want to consume the event
If KeyCode = KeyCodes.KEYCODE_BACK Then
  If Msgbox2("Do you want to exit?", "", "Yes", "", "No", Null) = DialogResponse.POSITIVE Then
    Return False
  Else
    Return True
  End If
End If
End Sub

Umberto
Hi udg,

With your code the app crashes too. Also crashes changing activity.finish for ExitApplication
 
Upvote 0

ivavilagu

Member
Licensed User
Longtime User
??? where ???

In the source of "udg" user there isn't activity.finish command

where you have change it ??

Sergio

sorry, I tried the udg code and the app crashes. I mean that changing the activity.finish line in the first post for ExitApplication the app also crashes.

If I don´t catch the button back event, the app exits without problem.
 
Upvote 0

nwhitfield

Active Member
Licensed User
Longtime User
you need to add a timer to reset the backcounter variable after passing 3 o 4 seconds. If you press for second time the back button having spent two minutes the app will exit without showing the message.

Well, yes, I suppose so; in my case, I reset that counter whenever pretty much anything is done, via other ways. But, however you choose to handle it, that will solve your problem.

More details in this thread: http://www.b4x.com/android/forum/threads/android-4-3-pb-with-closing-the-app.31328/

It was 4.3 which introduced this issue, which is why things work in your emulator but not on the devices.
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Hi,

found same thread already showed by Nigel in his post#8.
So following those directions should solve the problem.

udg
 
Upvote 0

ivavilagu

Member
Licensed User
Longtime User
Yeah, it works with the Erel code.

Thanks to everyone!
 
Upvote 0
Top