LocationFragment.java
LocationActivity activity;
onCreateView(){
mFusedLocationClient.getLastLocation()
.addOnSuccessListener(activity, new OnSuccessListener<Location>(){
@Override
public void onSuccess(Location location) {
if(location != null){
// update UI here ..
}
}
}
);
}
onAttach(){
activity = (LocationActivity) context;
}
这是将活动传递到Google Location API的正确方法吗?如果是这样,为什么?如果有内存泄漏等正当理由请解释一下吗? 提前致谢。抱歉懒惰打字! :)
答案 0 :(得分:-1)
使用getActivity()
在Fragment中获取Activity上下文。
addOnSuccessListener(getActivity(), new OnSuccessListener<Location>(){
.....
}