Android Question How can I stop an activity?

mark35at

Well-Known Member
Licensed User
I have two activities, Main and a calculation (can be found in this post http://www.b4x.com/android/forum/threads/how-to-unload-an-image-to-free-memory.50727/ ).

I thought everything was working well but now I have a new problem. Main allows the selection of two points on a map, these points are passed to the second activity. This activity finds the route between the two points.

The problem is, IF I make a wrong selection , I want to stop the second activity and go back to the first. The calculations are pretty intensive. Pressing the back button crashes the whole app with a single error message in the log (will post it when I get home - sorry).

Addiing a DoEvents does not help, as the calculations are then way too slow.

Thus my question in the title.
Many thanks for any help.
 

lemonisdead

Well-Known Member
Licensed User
Hello,
Could the calculation be done in a service ? It could be easier to stop a service when you press the Back button.
 

mark35at

Well-Known Member
Licensed User
I did think about using a service but wanted to get the activity finished first i.e. working correctly.
 

Troberg

Well-Known Member
Licensed User
Have you tried not adding the DoEvents at the innermost level of the calculations? Or limit it so that it only runs if it's a new second?
 

mark35at

Well-Known Member
Licensed User
with: depending on distance to compute, up to 5 minutes
without: less than 1 minute, with optimisation less than 30 seconds.
 

Troberg

Well-Known Member
Licensed User
Even better, check system time and only do a DoEvents when a certain time has passed (I usually do it once per second). That way, the DoEvents will tick at the same pace, regardless of device performance.
 

mark35at

Well-Known Member
Licensed User
@Erel, whole activity is in the link in post #1. A DoEvents after x iterations is difficult as there is no for/next loop but a do while loop.
I could prevent stopping by trapping the back key but that would mean waiting for the activity to finish however long it took.

The system time might be an option as i measure the time for the calculations for debugging!
 

Troberg

Well-Known Member
Licensed User
Use the time method

Something like:

B4X:
Dim PrevTime as long
do Until CalcDone
  'Calculate...

  if DateTime.Now-PrevTime>1000 then
    PrevTime=Now
    DoEvents
  end if
loop
Or, simply keep a separate counter to do it every X iterations.
 

mark35at

Well-Known Member
Licensed User
Thank you Troberg for your help but I am pretty sure that that will not work. If you look at my code from post#1, the sub BeginPathfinder you will see, the do loop is executed an irregular number of times. I could be blind but I cannot see how to add your code.

Just a bit of futher information, here are some actual timings.

Emulator: Breaks after 5 minutes as it cannot cross a road. This is typical with the emulator, rarely happens on a real device.
Samsung Galaxy Tab 2: 0.255 seconds for 30 iterations to calculate 25 markers between the points in the screenshot (taken from emulator not Samsung, markers not shown).
 

Attachments

Troberg

Well-Known Member
Licensed User
Just add my code somewhere where it will be run often. Make sure you have the dim outside the calc loop, and the if where it runs often.

If more than a second has passed since the last time the if was encountered, a doevents will be done. How the code is structured is not important, as long as it's run pretty often.
 
Top