1

I have developed an android app which uses Yahoo! Weather API for getting weather information, as Yahoo! Weather API allows to request the service only 10 to 20 times per hour. my app has tow activities when I switch back and forth between them my app requests the weather service again and again my code for this is :

@Override
protected void onResume() {
    // TODO Auto-generated method stub      
    String nameOfCity= pref.getString("cityname", cityNameInput.getText().toString());      
    getWeather(null, nameOfCity);// this the AsyncTask which is used to request Yahoo! Weather service.
    cityNameInput.setText(nameOfCity);
    super.onResume();
    Log.d("My_TAG", "in onResume()");
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub      
    editor = pref.edit();
    if(cityNameInput.getText().toString().equals("")){
        cityNameInput.setText(yahooWeatherInfo.getLocationCity());
    }else{
        editor.putString("cityname", cityNameInput.getText().toString());
    }
    editor.commit();
    super.onPause();
    Log.d("MY_TAG", "in onPause()");
}

Now please tell me how to restrict that not to request the Yahoo! Weather Service again and again on switching between activities.

Arshad Ali
  • 3,082
  • 12
  • 56
  • 99

1 Answers1

1

I looked at you code.

You are calling getWeather(null, nameOfCity); in onResume(). This onResume() method is called when your activity resume once again.

For ex: A ---> B then Press back from B activity then A activity onResume() method will be called and its makes request to server. So call this method `getWeather(null, nameOfCity); in onCreate(bundle). which is called only once when activity is created.

Ajay S
  • 48,003
  • 27
  • 91
  • 111
  • Friend first thanks for your precious consideration to my problem, I have tried according to your suggestion to the `getWeather(null, nameOfCity);` in `OnCreate()` but still app is requesting again and again to server. – Arshad Ali Mar 10 '13 at 06:52
  • yes I have removed `getWeather(null, nameOfCity);` from `onResume()` .. but still same problem! – Arshad Ali Mar 10 '13 at 06:54
  • @ArshadAliArshay You need to debug the application some where in your code bug that continue making server call. – Ajay S Mar 10 '13 at 06:56