Android Question How can I stop an activity?

Discussion in 'Android Questions' started by mark35at, Feb 19, 2015.

  1. mark35at

    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.
     
  2. lemonisdead

    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.
     
  3. mark35at

    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.
     
  4. Troberg

    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?
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    How slow are the calculations (without DoEvents)?
     
  6. mark35at

    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.
     
  7. Erel

    Erel Administrator Staff Member Licensed User

    Can you post these calculations?

    BTW, one possible solution is to call DoEvents every x iterations.
     
  8. Troberg

    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.
     
  9. mark35at

    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!
     
  10. Troberg

    Troberg Well-Known Member Licensed User

    Use the time method

    Something like:

    Code:
    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.
     
    Erel likes this.
  11. mark35at

    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).
     

    Attached Files:

  12. Troberg

    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.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice