Android Question Determining which activity is closing

Gaver Powers

Member
Licensed User
Longtime User
I have a problem where the back button is closing the app when it should be returning the app to the main display.

The main display includes a button for viewing an address on Google Maps - which is loaded using a webview.

If the user clicks the back button on the phone - it closes the app rather than returning the app to the main screen.

Is there a way to end the webview without closing the main app, without having to create a stand-alone activity? Perhaps by detecting which activity is closing and "if webview then show main" ?

G
 

LucaMs

Expert
Licensed User
Longtime User
Probably you can use this in every activity:
B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        StartActivity(Main)
        Return True
    End If
    Return False
End Sub

or

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK Then
        StartActivity(Main)
        Activity.Finish
        Return True
    End If
    Return False
End Sub
 
Upvote 0

Gaver Powers

Member
Licensed User
Longtime User
Thank you for your help Luca,

I'm getting the following error message.
Undeclared variable 'main' is used before it was assigned any value.

Also... I have other activities on the main display that can be launched.
Is there a way to detect which activity is being viewed and if webview then intercept the back button event?

G
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Thank you for your help Luca,

I'm getting the following error message.
Undeclared variable 'main' is used before it was assigned any value.

Also... I have other activities on the main display that can be launched.
Is there a way to detect which activity is being viewed and if webview then intercept the back button event?

G

That is not a really error, it is a warning; but you should not get it, because Main is the name of the main activity.

Webview does not intercept the back button event, an activity can do it.

You should use a global variable like "Public CurrentAct As Activity" in the Main Activity, set it in every Activity_Create and check it from anywhere you need.

Wrong

You could use a global variable but with the name of the current activity and set it in the Activity_Resume routine.

I have attached an example
 

Attachments

  • lm CurrentActivity.zip
    13.4 KB · Views: 114
Last edited:
Upvote 0

Gaver Powers

Member
Licensed User
Longtime User
Thanks again Luca,

I need to revisit how I'm declaring all my activities - currently I have all of the Activity processes under the Activity_Create(FirstTime as Boolean).
Splitting them up into their own Activity_Create subroutines will probably make this alot easier to manage.

In the interim I got this to work for me...

B4X:
Sub Activity_KeyPress(KeyCode As Int) As Boolean 'Return True to consume the event
    If KeyCode = KeyCodes.KEYCODE_BACK AND WebView1.Visible = True Then
        WebView1.Visible = False
        Return True
    End If       
    Return False
End Sub

It leaves the webview in place - just hides it from view - which by the way - really speeds up the display when I return to it.

G
 
Upvote 0

James Chamblin

Active Member
Licensed User
Longtime User
Are you actually creating a new activity with "Project->Add New Module->Activity Module" or are you just loading a new layout or view over the first? There is a difference between Activities, Layouts, and Views. The back key defaults to closing the topmost Activity, so if you are just covering it up with a view, then the entire activity will be closed.

Read the beginner's guide Chapter 13.2 for an example of using multiple activities.

There are times when opening a new activity isn't what you need, If you just need to open the one WebView temporarily, then the method you are using will probably work just fine. However, from your first post, it sounds like you have several of these views that need to be opened at various times. this could cause complications, especially if the orientation changes or the app is sent to the background (phone call comes in, another app started, etc...), then you would need to keep track of all the views' state. It might be more feasible to create a new Activity module for each of these views.
 
Upvote 0

Gaver Powers

Member
Licensed User
Longtime User
I'll go read the Guide and see what I can do to get this more organized.

Yes, I'm loading views on top of the main activity.

I tried creating a separate activity for the webview -> map process - but I needed to intercept a button click on the map (Call) and then destroy the webview and launch the phone dialer. I couldn't figure out how to pass the phone number back to the phone dialer and bring it to the front of the view. It was launching the phone dialer ok - but I couldn't hide the webview.

Just noticed your sample file attached above - I'll go check it out too.
Thanks again,

G
 
Upvote 0
Top