App Closes When Pressing Back Key?

disit10

Member
Hey guys my app closes whenever I hit the back key. I was wondering how you can press the back key and return to the previous activity?
 

Kevin

Well-Known Member
Licensed User
Longtime User
Android should automatically return to your previous activity assuming you didn't call Activity.Finish prior to starting the other activity.

You can get fancy and track activities and trap the back key with

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
  If KeyCode = KeyCodes.KEYCODE_BACK Then
    ... start another activity based on your custom tracking
    Return True  'Consume the Back key and don't allow Android to process it by iteself
  End If
End Sub

But again, you shouldn't have to get that fancy in most cases. However, if you are not actually starting another activity but rather you are showing and hiding panels then you will need to trap the back key as shown depending on whether or not you have a panel visible.
 
Upvote 0

Kevin

Well-Known Member
Licensed User
Longtime User
What I posted is pretty much all there is to it. If you are in fact using panels as opposed to activities then it could be a simple matter of adding a check for panel visibility as below:

B4X:
Sub Activity_KeyPress (KeyCode As Int) As Boolean
  If KeyCode = KeyCodes.KEYCODE_BACK Then
    If MyOtherPanel.Visible = True then
      MyOtherPanel.Visible = False
      Return True  'Consume the Back key and don't allow Android to process it by iteself
    End if
  End If
  
  'If it gets here, the app will exit as usual
End Sub
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
What version of Android are you testing your app on?

I found recently that an app worked as expected on Gingerbread - the back key would return to the previous Activity - but on an ICS device the back key would sometimes exit the app and sometimes return to the previous Activity.

I fixed it by adding android:targetSdkVersion="8" to the manifest file.

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
'End of default text.

You might want to use a different target version than 8 - my app's minSdkVersion was 8 so that's why i set the targetSdkVersion to 8.

Martin.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I am testing on 4.0.4

Try the manifest edit and see if that works - or do you need to target ICS rather than Gingerbread or earlier?

I did find prior to editing my manifest file, that if i tapped my Activity then used the back key it would more often than not return to the last Activity.

Strange - guess the tap gives the current Activity the focus so the back key works as desired.

Martin.
 
Upvote 0

disit10

Member
Try the manifest edit and see if that works - or do you need to target ICS rather than Gingerbread or earlier?

I did find prior to editing my manifest file, that if i tapped my Activity then used the back key it would more often than not return to the last Activity.

Strange - guess the tap gives the current Activity the focus so the back key works as desired.

Martin.

I will try it really quick and let you know if it works.
 
Upvote 0
Top