Android Question How to work with two activities and a REST Service.

Discussion in 'Android Questions' started by jotaele, Jun 7, 2015.

  1. jotaele

    jotaele Member Licensed User

    Hi all

    I have two activities in an app. Activity1 and Activity2

    The Activity1 calls to Activity2 clicking in a button.

    The Activity2 calls a REST service with this code:

    Code:
    Sub ObtenerObjetivo()

       jobMapa.Initialize(
    "ObtenerObjetivo", Me)
       jobMapa.PostString(
    "http://somedomain.com/REST/myapp/v1/obtenercoordenadasobjetivo","idgrupo=" & InicioServicios.globalIdGrupo)
       jobMapa.GetRequest.SetHeader(
    "Authorization", InicioServicios.globalApiKey)

    End Sub

    Sub JobDone (job As HttpJob)

      
    If job.Success = True Then
         
    Select job.JobName

           
    Case "ObtenerObjetivo"
      
           
    Try
           ...
           
    End Try

       
    End If

    End Sub
    Well, the problem is than if you exit the Activity2 pressing the BACK KEY, or with a button, ... if the respones of the REST server returns to the app, when you are in the Activity1, the app show the Activity2, even its closed.

    I tried to use a variable two know if the Activity2 is shown but not works. The IsPaused didn't work too.

    Anyone can help me?

    Thanks in average.
     
    Last edited: Jun 7, 2015
  2. Erel

    Erel Administrator Staff Member Licensed User

    The solution is to use a service for this. The service will not be paused. If the relevant activity is in the background then store the data until the activity is resumed.
     
  3. jotaele

    jotaele Member Licensed User

    (Sorry for my bad English)

    I think that you dont understood the problem or I didn't explain well. Don't get me wrong.

    For example, imagine that the first Activity1 have a textbox and you write something. When you click the button, opens the Activity2 and the Activity2 makes the REST request to the server, and show the data.

    Well, If you exit the Activity2 very fast, the Activity2 goes to pause, but it takes time to close the Activity2. In this time, the value for Ispaused(Me), is false, but goes to true in 1 second or less.

    If in this interval of time, the response of the server reach the app, Activity2 goes to foreground.

    The problem is this. IsPaused takes time to change the value.

    I dont undestand how to use a service for do this.

    I tried this, but not works.

    Code:
    Sub JobDone (Job As HttpJob)
       
       
    If Job.Success = True AND Not(IsPaused(Me)) Then
          
    Select Job.JobName
             
    Case "jobListarGrupos"
             
                 ...
    Is there another method to know if the Activity is in foreground?
     
  4. Erel

    Erel Administrator Staff Member Licensed User

    I understood the problem. It happens because HttpUtils2 internally uses CallSubDelayed to raise the JobDone event. If you read the tutorial about CallSubDelayed you will see that it automatically starts the activity (if the app is in the foreground). This is the reason for Activity2 being started again. It is not related to IsPaused.

    You should go over the services tutorial and learn how to use services.
     
  5. jotaele

    jotaele Member Licensed User

    Ok. I now understand the problem. As you said, HttpUtils2 internally uses CallSubDelayed.

    Its not a problem use services. I use them in my apps without problem.

    I understand then, that I must make the petition to the REST throught a Service, and then, the service must use the IsPaused method to know if the activity is in the foreground to show the info.

    Thanks a lot. I'm going crazy with this problem.
     
  6. Erel

    Erel Administrator Staff Member Licensed User

    More or less correct. With the service you have full control. You can decide to start the activity or to keep the data and only use it when the activity is resumed.
     
  7. jotaele

    jotaele Member Licensed User

    I tried and works fine. Thanks a lot.
     
Loading...