2 回答

TA貢獻(xiàn)1865條經(jīng)驗(yàn) 獲得超7個(gè)贊
為什么不將狀態(tài)存儲(chǔ)在 DescriptionFragment 中。
/**
* A simple {@link Fragment} subclass.
*/
public class DescriptionFragment extends Fragment {
private static final String DESCRIPTION = "DESCRIPTION";
private static final String CITY = "CITY";
TextView cityName;
TextView text;
public DescriptionFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_description, container, false);
}
@Override
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
text = view.findViewById(R.id.city_description);
cityName = view.findViewById(R.id.city_name);
if (savedInstanceState != null) {
String description = savedInstanceState.getString(DESCRIPTION);
String city = savedInstanceState.getString(CITY);
change(description, city);
}
}
@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
super.onSaveInstanceState(outState);
// Save the fragments Instance
outState.putString(DESCRIPTION, text.getText().toString());
outState.putString(CITY, cityName.getText().toString());
}
public void change(String description, String city) {
text.setText(description);
cityName.setText(city);
}
}

TA貢獻(xiàn)1898條經(jīng)驗(yàn) 獲得超8個(gè)贊
可以使用您的xml AndroidManifest.xml 中的屬性
android:configChanges="keyboardHidden|orientation"
在這里,它將向應(yīng)用程序表明您將負(fù)責(zé)處理輪換,并且不會(huì)重新啟動(dòng)您的活動(dòng),以便不存在空數(shù)據(jù)。
添加回答
舉報(bào)