如何从活动更新片段

时间:2017-12-26 10:45:47

标签: android android-fragments android-activity

我正在创建一个包含2个片段的应用程序,这些片段占据整个屏幕并由viewpager管理,它们都使用位置数据,我通过实现LocationManager获取位置数据,并使用LocationListener检查用户是否已移动和需要新数据。两个片段都使用用户位置来显示初始数据,并且两者都需要知道用户是否已更改位置以显示新数据。我使用以下代码获取位置数据并请求更新:

    private void getLocationData() {
    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("LOLWeather", "onLocationChange() callback received");
            mLatitude = location.getLatitude();
            mLongitude = location.getLongitude();                
        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {
        }
        @Override
        public void onProviderDisabled(String provider) {
            Log.d("LOLWeather", "onProviderDisabled callback received");
        }
    };

    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_COARSE_LOCATION} ,REQUEST_CODE);
        return;
    }
    mLocationManager.requestLocationUpdates(LOCATION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
    mLatitude = mLocationManager.getLastKnownLocation(LOCATION_PROVIDER).getLatitude();
    mLongitude = mLocationManager.getLastKnownLocation(LOCATION_PROVIDER).getLongitude();
}

我的第一个问题是我想我可以将这些代码放在每个片段中,然后在每个片段中使用onPause:             if(mLocationManager!= null)mLocationManager.removeUpdates(mLocationListener);

然而,每次用户从一个片段/视图切换到另一个片段/视图时,请求位置数据似乎是错误的,并且每次都要添加和删除locationListener。既然他们都使用位置数据,我认为最好将getLocationData()方法放在活动本身中,这是正确的吗?如果不是我应该如何处理它,将getLocationData()方法放在每个片段???

我的后续问题,除非我错了,这就是我卡住的地方,现在当在locationListener中调用onLocationChanged()方法时,我在主活动中有getLocationData()方法如何告诉片段该位置改变所以它更新自己???我最初通过bundle将数据传递给片段,但是现在片段已经创建了,我可以在片段中创建一个方法,一旦它有新数据就更新它,但我怎么知道它必须更新自己并给出它是新的位置数据???

感谢。

2 个答案:

答案 0 :(得分:1)

正确的方式在我看来 将函数getLocationData()放在Activity中,然后从那里发送一个具有新位置的Broadcast。

这是关于broadcat的解释链接。

https://developer.android.com/guide/components/broadcasts.html

答案 1 :(得分:1)

  

由于他们都使用位置数据,我认为最好将getLocationData()方法放在活动本身中,这是正确的吗?

我认为这是一个很好的方法。

  

如何告诉片段位置已更改,以便自动更新???

您可以在捕获位置更新时发送广播,并将接收者注册到此事件以更新您的片段。

e.g:

当您在活动的方法中捕获更新事件时:

sendBroadcast(new Intent("action_location_updated"));

然后,在你的片段中:

BroadcastReceiver mReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if ("action_location_updated".equals(intent.getAction())) {
                // update fragment here
            }
        }
    };
IntentFilter filter = new IntentFilter("action_location_updated");
registerReceiver(mReceiver, filter);

注意:最好将动作的字符串用作静态最终变量:

public static final ACTION_UPDATE_FRAGMENT = "action_location_updated";
相关问题