Android Question Change activity to code module or service?

Mark Read

Well-Known Member
Licensed User
Longtime User
I have two activities in my app.

Activity 1: Has a working OSM Map with all functions. The user can select two points on the map and get the route between them.

Activity 2: Takes the two points (from global variables in activity 1), calculates the route and returns a list of geopoints back to activity 1, again via a global variable.

My question is in two parts.
1. Is there any benefit of changing the second activity to a service or class? I am thinking along the lines of the HttpUtils.bas module. There is no interaction from the user and nothing really to see. The code is quite long but its running time is about 3 seconds.
2. I would like to display a message in activity 1 telling the user to wait for the calculation but it cannot be seen once activity 2 is active.

Thanks in advance.

Edit: What I had in mind was something along the lines of Erel's Action List where I can dim an object, set its properties and then run the sub to do the calculation. This is a code module. Will that also slove my problem 2?
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
The only reason that ActionList is implementation with a code module instead of a class is that at the time of writing this code, classes were not supported.

Classes are more powerful and flexible than code modules.

If the code doesn't interact with the UI then it should probably be implemented in a class. Whether this class belongs to a service or the same activity depends on your requirements.

You should only start a second activity if you want to show a new page to the user.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Activity 2 presently does not interact with activity 1 but it requires a webview which can be invisible. Activity 2 controls a webpage and obtains data from it Theoretically I could hide the webview in activity 1 and then send data to and from it with a class. Or have I not understood properly?

The other option is to leave everything as it is and hide a webview in Activity 2 and just display a "please wait" message in a label...
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
Thanks Erel. Will look into that. Last night I noticed a new problem using these two activities.

In activity 1 _Create (at the moment for testing purposes) I call activity 2 after initializing a variable required in activity 2. So far so good.
Activity 2 begins, starts the httputils service to download a file -also okay BUT..
Before activity 2 starts, Activity 1 runs the "resume" sub which contains some code and breaks the download job. The app just hangs, no error, nothing.

It seems I am backing myself between a rock and a hard place by using two activities. I assume this is normal but how can I stop it? If I understand correctly, after finishing activity 2, activity 1 resume should be called or not?

Many thanks.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Before activity 2 starts, Activity 1 runs the "resume" sub which contains some code and breaks the download job. The app just hangs, no error, nothing.
If you can reproduce it then please upload a small example.

If I understand correctly, after finishing activity 2, activity 1 resume should be called or not?
If activity 1 was behind activity 2 then activity_Resume should be called.

As I previously it will be simpler to use a service, especially for the http request.
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
I cannot reproduce it with an example. Here is my app to date and a copy of the log. I have no idea where I am in the app at this stage!

Thanks in advance.
 

Attachments

  • Waterway.zip
    10.9 KB · Views: 136
  • log.jpg
    log.jpg
    119.3 KB · Views: 105
Upvote 0

JonPM

Well-Known Member
Licensed User
Longtime User
What is the purpose of loading a second activity immediately after Main activity loads?
 
Upvote 0

Mark Read

Well-Known Member
Licensed User
Longtime User
The app is in the early stages of development. Normally I would display the map in activity 1 and let the user select two points. These points are passed to activity 2 to calculate the route and then the route is passed back to the map in activity 1. At present, the two points are "hardwired" into activity 2 (in Activity_Create - LoadUrl, the lon & Lat values are fixed) and the webview is visible, normally the user will only see a "please wait message".

If the code doesn't interact with the UI then it should probably be implemented in a class. Whether this class belongs to a service or the same activity depends on your requirements.

You should only start a second activity if you want to show a new page to the user.

Erel's answer in post #6 says a service instead of activity 2 would be better. I wonder if a panel with a webview (required), placed over the map in activity 1 might be a better idea.

I would be grateful for any help.
 
Upvote 0
Top