[英]getArguments().getString always returns null in my Fragment
I have a following problem. 我有以下问题。 I would like to save the values of some
TextView
s so that they can be retrieved and displayed after reutrning back to this Fragment
(I use replace()
method to switch Fragment
s). 我想保存一些
TextView
的值,以便在返回到此Fragment
后可以检索和显示它们(我使用replace()
方法切换Fragment
)。 I followed the advice from this POST , so my code looks like this right now: 我遵循了POST的建议,所以我的代码现在看起来像这样:
public class NumbersFragment extends Fragment {
private static final String LONGITUDE_SAVE = "longitudeSave";
private static final String LATITUDE_SAVE = "latitudeString";
public TextView tvLatitude;
public TextView tvLongitude;
public Button startButton;
public Button stopButton;
public NumbersFragment() {
setArguments(new Bundle());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.numbers_fragment, container, false);
tvLatitude = (TextView) rootView.findViewById(R.id.tv_latitude);
tvLongitude = (TextView) rootView.findViewById(R.id.tv_longitude);
startButton = (Button) rootView.findViewById(R.id.start_button);
stopButton = (Button) rootView.findViewById(R.id.stop_button);
refreshUI();
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().startService(new Intent(getActivity(), RecordService.class));
}
});
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().stopService(new Intent(getActivity(), RecordService.class));
}
});
//getActivity().setTitle(R.string.record);
LocalBroadcastManager.getInstance(getActivity().getApplicationContext()).registerReceiver(
new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
double latitude = intent.getDoubleExtra(RecordService.EXTRA_LATITUDE, 0);
double longitude = intent.getDoubleExtra(RecordService.EXTRA_LONGITUDE, 0);
//textView.setText("Lat: " + latitude + ", Lng: " + longitude);
//tvLocation.setText("Latitude" + Double.toString(latitude) + "Longitude" + Double.toString(longitude));
tvLongitude.setText("Longitude: " + Double.toString(longitude));
tvLatitude.setText("Latitude: " + Double.toString(latitude));
}
}, new IntentFilter(RecordService.ACTION_LOCATION_BROADCAST)
);
return rootView;
}
@Override
public void onPause() {
super.onPause();
String latitudeToSave = tvLatitude.getText().toString();
String longitudeToSave = tvLongitude.getText().toString();
getArguments().putString(LATITUDE_SAVE, latitudeToSave);
getArguments().putString(LONGITUDE_SAVE, longitudeToSave);
Log.d("onPause", latitudeToSave + " " + longitudeToSave);
}
@Override
public void onResume() {
super.onResume();
refreshUI();
}
public void refreshUI() {
Bundle mySavedInstanceState = getArguments();
String loadedLatitude = mySavedInstanceState.getString(LATITUDE_SAVE);
String loadedLongitude = mySavedInstanceState.getString(LONGITUDE_SAVE);
Log.d("refreshUI", loadedLatitude + " " + loadedLongitude);
tvLatitude.setText(loadedLatitude);
tvLongitude.setText(loadedLongitude);
}
}
The problem is that after returning to the Fragment
refreshUI method is called, but loadedLatitude
and loadedLongitude
String
s are always null
. 问题在于,返回到
Fragment
之后,会调用refreshUI方法,但loadedLatitude
和loadedLongitude
String
始终为null
。 What am I doing wrong? 我究竟做错了什么?
Pass Bundle savedInstanceState to refreshUI() method 将捆绑包saveInstanceState传递给refreshUI()方法
Correct code : refreshUI(savedInstanceState); 正确的代码:refreshUI(savedInstanceState);
Receive it in method and use it : 接收方法并使用它:
refreshUI(Bundle a) { String loadedLatitude = a.getString(LATITUDE_SAVE) refreshUI(Bundle a){字符串加载的纬度= a.getString(LATITUDE_SAVE)
This will work 这会起作用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.