Android的片段:用于Web服务调用它的生命周期方法 [英] Android Fragment : which life cycle method to use for web service call

查看:142
本文介绍了Android的片段:用于Web服务调用它的生命周期方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开发,其中几个片段所涉及的应用程序。
在每个片段中我要调用Web服务来获取数据。

I am developing an application in which several fragments are involved. In each fragment I have to call web service to fetch data.

目前我从 onCreateView()片段的方法调用Web服务。问题我得到每当Web服务调用过程中,如果设备的方向发生改变,然后新的Web服务调用开始调用。

Currently I am calling web service from onCreateView() method of Fragment. Issue i am getting that whenever web service call is in progress and if device orientation is changed then new web service call starts invoking.

我想这可能是因为onCreateView()方法被调用的配置更改。

I think this might be because onCreateView() method gets called on configuration change.

我怎样才能解决这个问题。和生命周期的方法,我应该用它来调用Web服务,以便它会被调用一次

How can I solve this. and which Life cycle method should I use to call web service so that it will be get called only once

推荐答案

我已经通过以下解决方法解决了这个

I have resolved this by following workaround


  1. 为每个Web服务调用方法的操作标识。例如。例如,验证登录调用

  1. Create an operation identifier for each web service call method. E.g. for example "Authentication" for login call

创建ArrayList的一个对象说currentTasks

Create one object of ArrayList say currentTasks

ArrayList<String> currentTasks = new ArrayList<String>();


  • 在我在哪里调用Web服务的每一个方法,检查相应的方法中的操作标识已经美元的ArrayList p $ psent。如果没有,那么开始运行。

  • In every method where I am calling web service, check if operation identifier of corresponding method is already present in ArrayList. If not then start operation.

    String operationId = "Authentication";
    if(currentTasks.indexOf(operationId) == -1)
    {
      <do web service call operation here>
       currentTasks.add(operationId);
    }
    


  • 方法,其上述操作的响应接收,从ArrayList中删除操作标识符

  • Method in which above operation's response is receiving, remove operation identifier from ArrayList

     if(currentTasks.indexOf("Authentication") != -1){
        currentTasks.remove("Authentication");
     }
    


  • 这将确保通话不会去这是目前正在进行中的Web方法。

    This will ensure that call will not go to web method which is currently in progress.

    我知道这是不是实现这一目标的最佳途径,这可能不是最好的做法,以遵循,但现在这对我的作品。

    I know this is not the best way to achieve it and this might not the best practice to follow but for now this works for me.

    这篇关于Android的片段:用于Web服务调用它的生命周期方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆