Всем привет! Есть два ShrelockFragment. В Fragment_map находится карта, там же происходит получение координат по GPS. В Fragment_control находятся несколько TextView, которые отображают полученные координаты.
При запуске приложения на телефоне, Fragment_control отображает координаты. Но как только перейти на другой фрагмент и вернуться обратно, то все TextView исчезают. Хотя координаты получаю и вижу на карте.
В чем проблема? Или как можно сделать постоянное обновление TextView из другого фрагмента?
Fragment_map.java:
package com.example.wimf;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import com.actionbarsherlock.app.SherlockFragment;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.SupportMapFragment;
public class Fragment_map extends SherlockFragment
{
public GoogleMap map;
private SupportMapFragment map_fragment;
public double latitude;
public double longitude;
public LocationManager locationManager;
public TextView gps_status;
public TextView gps_mode;
String disabled, enabled, mode;
public TextView get_latitude;
public TextView get_longitude;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_map, container, false);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
map_fragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);
map = map_fragment.getMap();
map.setBuildingsEnabled(true);
map.setMyLocationEnabled(true);
gps_status = (TextView)getSherlockActivity().findViewById(R.id.tv_gps_status);
gps_mode = (TextView)getSherlockActivity().findViewById(R.id.tv_gps_mode);
get_latitude = (TextView)getSherlockActivity().findViewById(R.id.tv_latitude);
get_longitude = (TextView)getSherlockActivity().findViewById(R.id.tv_longitude);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
@Override
public void onStart()
{
super.onStart();
try
{
checkEnabled();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 1, locationListener);
}
catch(Exception ex)
{
System.out.println("[fr_map_onStart()]ERROR:" + ex);
}
}
private LocationListener locationListener = new LocationListener()
{
@Override
public void onLocationChanged(Location location)
{
try
{
if (location == null)
return;
if (location != null && location.getProvider().equals(LocationManager.GPS_PROVIDER))
{
latitude = location.getLatitude();
longitude = location.getLongitude();
get_latitude.setText("Широта: " + String.valueOf(latitude));
get_longitude.setText("Долгота: " + String.valueOf(longitude));
}
}
catch(Exception ex)
{
System.out.println("[fr_map_onLocationChanged()]ERROR:" + ex);
}
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
try
{
if (provider.equals(LocationManager.GPS_PROVIDER))
{
mode = String.valueOf(status);
gps_mode.setText("Режим работы GPS: " + mode);
}
}
catch(Exception ex)
{
System.out.println("[fr_map_onStatusChanged()]ERROR:" + ex);
}
}
@Override
public void onProviderEnabled(String provider)
{
try
{
checkEnabled();
}
catch(Exception ex)
{
System.out.println("[fr_map_onProviderEnabled()]ERROR:" + ex);
}
}
@Override
public void onProviderDisabled(String provider)
{
try
{
checkEnabled();
}
catch(Exception ex)
{
System.out.println("[fr_map_onProviderDisabled()]ERROR:" + ex);
}
}
};
private void checkEnabled()
{
try
{
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) == true)
{
enabled = "включен";
gps_status.setText("Статус GPS: " + enabled);
}
if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) == false)
{
disabled = "выключен";
gps_status.setText("Статус GPS: " + disabled);
}
}
catch(Exception ex)
{
System.out.println("[fr_map_checkEnabled()]ERROR:" + ex);
}
}
}
Fragment_control.java:
package com.example.wimf;
import java.text.DateFormat;
import java.util.Date;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragment;
public class Fragment_control extends SherlockFragment
{
public TextView tv_date_now;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_control, container, false);
tv_date_now = (TextView)view.findViewById(R.id.tv_date);
return view;
}
@Override
public void onSaveInstanceState(Bundle outState)
{
super.onSaveInstanceState(outState);
setUserVisibleHint(true);
}
@Override
public void onStart()
{
super.onStart();
try
{
String now_date = DateFormat.getDateInstance(DateFormat.FULL).format(new Date());
tv_date_now.setText(now_date);
}
catch(Exception ex)
{
System.out.println("[fr_control_onStart()]ERROR:" + ex);
}
}
}
Fragment_control.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:text="@string/date_now" />
<TextView
android:id="@+id/tv_date"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<DigitalClock
android:id="@+id/clock"
android:textSize="16sp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="24sp"
android:textStyle="bold"
android:text="@string/coordinates" />
<TextView
android:id="@+id/tv_latitude"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_longitude"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_gps_status"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/tv_gps_mode"
android:textSize="18sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>