我正在Android中构建一个测试应用程序,问题是当我改变屏幕的方向时,片段再次被加载并且数据消失了,这是从Json请求的。
这是请求代码
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String testDetailUrl = "http://www.json-generator.com/api/json /get/cqZnauiJbC?indent=2";
((TrikshaApplication) parentActivity.getApplication()).setTestTime(0);
TaskResponseListener taskResponseListener = new TaskResponseListener();
JsonRequest messageRequest = new JsonRequest(testDetailUrl, testQuestions[].class,
taskResponseListener, taskResponseListener);
uiHandler = new Handler();
arrayList = new ArrayList<String>();
if (!((TrikshaApplication) parentActivity.getApplication()).getCallingFragment().equals("Shiksha")) {
timeLimitLauncher = new TimeLimitLauncher();
}
this.getServiceLocator().getRequestQueue(getActivity()).add(messageRequest);
}
以下是配置更改代码
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
LayoutInflater inflater = LayoutInflater.from(getActivity());
populateViewForOrientation(inflater, (ViewGroup) getView());
getActivity().setContentView(R.layout.fragment_test_question_ans_layout);
}
private void populateViewForOrientation(LayoutInflater inflater, ViewGroup viewGroup) {
viewGroup.removeAllViewsInLayout();
View subview = inflater.inflate(R.layout.fragment_test_question_ans_layout, viewGroup);
}
以下是我在活动清单文件中所做的更改
机器人:configChanges = “取向|屏幕尺寸| keyboardHidden”
以下是方向更改http://s3.postimg.org/gjfws3nxv/Screenshot_2015_06_12_12_08_02.png
之前的屏幕截图链接改变方向后 http://s4.postimg.org/86fgaxrwd/Screenshot_2015_06_12_12_08_08.png
我在layout-land文件夹中制作了两个布局,一个用于纵向,一个用于横向。