2016-05-21 90 views
0

我使用openweathermap api併成功地檢索了溫度,城市名稱,天氣圖標等json數據。但是,當我旋轉屏幕時,web服務再次運行,這不應該發生。片段方向

我所做的是以下幾點。我保存狀態或片段。

public class MainActivity extends AppCompatActivity implements 
GoogleApiClient.OnConnectionFailedListener, 

GoogleApiClient.ConnectionCallbacks{ 
private static final int PLAY_SERVICES_CODE = 90; 
GoogleApiClient googleApiClient; 
private static final String TAG = "Location"; 
private static final String TODAY_FRAGMENT = "today_fragment"; 

public static double lat = 0; 
public static double log = 0; 

TodayForecast ff; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FragmentManager fragmentManager = getSupportFragmentManager(); 
    ff = (TodayForecast) fragmentManager.findFragmentByTag(TODAY_FRAGMENT); 

    if (savedInstanceState != null) { 
     //Restore the fragment's instance 
     ff = (TodayForecast) 
    getSupportFragmentManager().getFragment(savedInstanceState, 
    "mContent"); 
    } 



    if(checkPlayServices()){ 
     initializeGoogleApiClient(); 
    } 
} 

private void initializeGoogleApiClient() { 
    googleApiClient = new GoogleApiClient.Builder(this) 
      .addApi(LocationServices.API) 
      .addOnConnectionFailedListener(this) 
      .addConnectionCallbacks(this) 
      .build(); 

} 

public boolean checkPlayServices(){ 
    int result = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext()); 

    if(result!= ConnectionResult.SUCCESS){ 
     if(GooglePlayServicesUtil.isUserRecoverableError(result)){ 
      GooglePlayServicesUtil.getErrorDialog(result, this, PLAY_SERVICES_CODE).show(); 
     } 
     return false; 
    } 
    return true; 
} 
@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    return super.onOptionsItemSelected(item); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    return super.onCreateOptionsMenu(menu); 
} 


@Override 
public void onConnected(@Nullable Bundle bundle) { 

    Location loc = LocationServices.FusedLocationApi.getLastLocation(googleApiClient); 

    if(loc!=null){ 

     //Log.v(TAG,"lat: "+lat); 
     //Log.v(TAG,"lon: "+log); 

     if(ff == null) { 

      lat = loc.getLatitude(); 
      log = loc.getLongitude(); 
      ff = new TodayForecast(); 


getSupportFragmentManager().beginTransaction().replace(R.id.main_container, 
TodayForecast.newInstance(lat,log),TODAY_FRAGMENT).commit(); 

     } 

    }else{ 
     Toast.makeText(getApplicationContext(),"No location 
     obtained",Toast.LENGTH_SHORT).show(); 
    } 
} 

@Override 
public void onConnectionSuspended(int i) { 

} 

@Override 
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { 

} 

@Override 
protected void onStart() { 
    super.onStart(); 
    googleApiClient.connect(); 
} 

@Override 
protected void onStop() { 
    super.onStop(); 

    if(googleApiClient.isConnected()){ 
     googleApiClient.disconnect(); 
    } 
    } 
} 

並在實際片段我用的方法的onSaveInstanceState檢索到的數據從我的web存儲。這是整個片段的代碼。

public class TodayForecast extends Fragment { 
private static String NEW_BASE_URL = "" ; 
public static String URL= "http://api.openweathermap.org/data/2.5/weather?"; 
public static String NEW_URL= "http://api.openweathermap.org/data/2.5/weather?q="; 
public static String BASE_URL= ""; 

public String time; 
public String setTime; 
String IMG_URL = "http://api.openweathermap.org/img/w/"; 
static double mlat; 
static double mlot; 
TextView cityText; 
ImageView imageView; 
private String cityName; 
private String humidity; 
private String pressure; 
private String speed; 
private String imageIcon; 
private String temperature; 
TextView tempTextView; 
TextView windTextView; 
TextView pressureTextView; 
TextView humidityTextView; 
TextView sunriseTextView; 
TextView sunSetTextView; 
Button btn; 

private String icon; 

public static TodayForecast newInstance(double lat, double log) { 
    TodayForecast fragment = new TodayForecast(); 

    mlat = lat; 
    mlot = log; 
    return fragment; 
} 

public TodayForecast() { 
    // Required empty public constructor 
} 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setHasOptionsMenu(true); 
} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View rootView = inflater.inflate(R.layout.fragment_today_forecast, container, false); 

    Log.v("Theo","lot: " + mlot); 
    Log.v("Theo","lat: " + mlat); 

    cityText = (TextView)rootView.findViewById(R.id.cityText); 
    imageView = (ImageView) rootView.findViewById(R.id.thumbnailIcon); 
    windTextView = (TextView) rootView.findViewById(R.id.windTextValue); 
    tempTextView = (TextView)rootView.findViewById(R.id.tempText); 
    pressureTextView = (TextView)rootView.findViewById(R.id.pressureTextValue); 
    humidityTextView = (TextView)rootView.findViewById(R.id.humidTextValue); 
    sunriseTextView = (TextView)rootView.findViewById(R.id.riseTextValue); 
    sunSetTextView = (TextView)rootView.findViewById(R.id.setTextValue); 

    btn = (Button)rootView.findViewById(R.id.change_city); 

    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 


      //showInputDialog(); 




     } 

    }); 
    //http://api.openweathermap.org/data/2.5/weather?lat=39.6304951&lon=22.4206619&appid=d48708e1e4d8e2b60da14778acd8d56a 
    BASE_URL = URL +"lat="+mlat+"&lon="+mlot+"&units=metric&appid=d48708e1e4d8e2b60da14778acd8d56a"; 
    if(savedInstanceState!=null) { 
     String city_name = savedInstanceState.getString("CityName"); 
     String city_temp = savedInstanceState.getString("CityTemp"); 
     String city_pressure = savedInstanceState.getString("CityPressure"); 
     String city_humidity = savedInstanceState.getString("CityHumidity"); 
     String wind_speed = savedInstanceState.getString("WindSpeed"); 
     String weather_icon = savedInstanceState.getString("weatherIcon"); 
     String sunrise_time = savedInstanceState.getString("sunrise"); 
     String sunset_time = savedInstanceState.getString("sunset"); 



     cityText.setText(city_name); 
     tempTextView.setText(city_temp); 
     pressureTextView.setText(city_pressure); 
     humidityTextView.setText(city_humidity); 
     windTextView.setText(wind_speed); 


     Picasso.with(getActivity()).load(weather_icon).into(imageView); 

     sunriseTextView.setText(sunrise_time); 
     sunSetTextView.setText(sunset_time); 


    }else{ 
     weatherData(); 

    } 

    //http://api.openweathermap.org/data/2.5/weather?q=Newcastle,uk&units=metric&appid=d48708e1e4d8e2b60da14778acd8d56a 


    return rootView; 



} 


private void weatherData() { 
    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET, 
      BASE_URL, 
      new Response.Listener<JSONObject>() { 


       @Override 
       public void onResponse(JSONObject response) { 

        Log.d("TAG", response.toString()); 
        try { 
         cityName = response.getString("name"); 
         cityText.setText(cityName); 
         //cityTextView.setText(cityName); 

         JSONObject main = response.getJSONObject("main"); 

         temperature = main.getString("temp"); 
         //String temperatureSbstr = temperature.substring(0, 2); 
         tempTextView.setText(temperature+""+"\u00b0 \u0043"); 

         humidity = main.getString("humidity"); 

         humidityTextView.setText(humidity + " %"); 

         pressure = main.getString("pressure"); 

         pressureTextView.setText(pressure + " hPa"); 

         JSONObject windValue = response.getJSONObject("wind"); 

         speed = windValue.getString("speed"); 

         windTextView.setText(speed + " km/h"); 

         JSONObject sysJSONObject = response.getJSONObject("sys"); 

         long sunrise = sysJSONObject.getLong("sunrise"); 
         Date dayTime = new Date(sunrise * 1000L); 

         SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss"); 
         time = timeFormat.format(dayTime); 

         sunriseTextView.setText(time); 

         long sunset = sysJSONObject.getLong("sunset"); 

         Date sunsetTime = new Date(sunset * 1000L); 

         SimpleDateFormat sunsettimeFormat = new SimpleDateFormat("HH:mm:ss"); 
         setTime = sunsettimeFormat.format(sunsetTime); 

         sunSetTextView.setText(setTime); 

         JSONArray weather = response.getJSONArray("weather"); 

         for(int i=0; i<weather.length();i++){ 

          JSONObject weatherJSONObject = weather.getJSONObject(i); 

          String mainWeather = weatherJSONObject.getString("main"); 

          //mainText.setText(mainWeather); 

          icon = weatherJSONObject.getString("icon"); 
          //Volley's Image request 
          ImageLoader imageLoader = AppController.getInstance().getImageLoader(); 

          imageIcon = IMG_URL + icon; 

          imageLoader.get(imageIcon, new ImageLoader.ImageListener() { 

           @Override 
           public void onErrorResponse(VolleyError error) { 

           } 

           @Override 
           public void onResponse(ImageLoader.ImageContainer response, boolean arg1) { 
            if (response.getBitmap() != null) { 
             // load image into imageview 
             imageView.setImageBitmap(response.getBitmap()); 
            } 
           } 
          }); 
          Log.d("icon", icon); 
         } 
        } catch (JSONException e) { 
         Log.e("TAG", e.toString()); 
        } 

       } 
      }, new Response.ErrorListener() { 

     @Override 
     public void onErrorResponse(VolleyError error) { 


     } 
    }); 
    // Adding request to request queue 
    AppController.getInstance().addToRequestQueue(jsonObjReq); 
} 




@Override 
public void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 

    outState.putString("CityName",cityName); 
    outState.putString("CityTemp",temperature); 
    outState.putString("CityPressure",pressure); 
    outState.putString("CityHumidity",humidity); 
    outState.putString("WindSpeed",speed); 
    outState.putString("sunrise",time); 
    outState.putString("sunset",setTime); 
    outState.putString("weatherIcon",imageIcon); 


} 


} 

您將看到存儲檢索數據後,我讀不懂他們的onCreateView(...)方法中,當Bundle對象(即savedInstanceState參考)不爲空。如果它表示沒有屏幕旋轉,那麼web服務正在運行。

當我必須在使用Parceable對象的recyclerview中顯示數據時,我解決了此問題。

任何想法,

謝謝!

回答

0

如果系統稍後必須重新創建活動實例,它會將同一個Bundle對象傳遞給onRestoreInstanceState()和onCreate()方法。 所以你可以檢索這兩種方法中的任何一種。 我還沒有測試解決方案。這是根據https://developer.android.com/training/basics/activity-lifecycle/recreating.html

+0

你不能在Fragmenst有onRestoreInstantStame()方法! – Theo

+0

我認爲這個職位回答你的問題。 http://stackoverflow.com/questions/20550016/savedinstancestate-is-always-null-in-fragment –