我有一个包含1个片段的主要布局。片段java类绑定了一些视图,当我第一次启动时,所有视图都是从onActivityCreate事件中正确填充的。 当我旋转手机时,我已经在片段中保留了所有数据,但我无法更新onActivityCreate事件中的任何视图或其他事件... 是否有包含视图的片段的旋转示例?
根据要求,我添加了添加片段的代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10">
<fragment
android:id="@+id/questionListFragment"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_weight="1"
class="com.sp1d3r.itil.itiltester.QuestionListFragment" />
<fragment
android:id="@+id/testFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="9"
class="com.sp1d3r.itil.itiltester.Test_Fragment" />
</LinearLayout>
在这种情况下我有两个片段,但问题是我无法在设备旋转后更新任何Test_Fragment视图
这里是片段类的代码
public class Test_Fragment extends Fragment {
private final static String TAG = "ITIL_TESTER";
private final static int SIXTY_MINUTES = 1000 * 60 * 60;
private final static int ONE_SECOND = 1000;
private final static int MIN_FLIP_DISTANCE = 100;
private RadioGroup radioGroup;
private TextView question_TV;
private Context mContext;
private DBHelper itildb;
private TextView answer1_RB, answer2_RB, answer3_RB, answer4_RB;
private LinearLayout ll;
private ScrollView scrollViewQA;
private ImageButton next_IB, prev_IB;
private ITIL_Test test;
private int idButtonPrev;
private FileToDB fdb;
private Test_Fragment tFragment;
private int maxQuestionNo;
private SwipeGestureListener gestureListener;
private TextView timerView;
private long mills;
private boolean loadFlag = false;
private CountDownTimer countDownTimer;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRetainInstance(true);
mLog("Pass from onCreate");
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedState) {
super.onActivityCreated(savedState);
mLog("Pass from onActivityCreated");
if (!loadFlag) {
initLayout();
itildb = new DBHelper(mContext);
maxQuestionNo = 40;
//Create a new test with 40 questions and 60 minutes
test = new ITIL_Test(this, maxQuestionNo, 60, itildb);
test.asynchLoadfromDB();
test.setCurrentQuestionNo(1);
loadFlag = true;
} else {
mLog("set the question!!!");
showQuestion(test.getCurrentQuestionNo());
mLog("Question no." + test.getCurrentQuestionNo());
}
mLog("FRAGMENT ID:"+String.valueOf(getId()));
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDestroy() {
Log.i(TAG, getClass().getSimpleName() + ":onDestroy()");
super.onDestroy();
}
@Override
public void onDestroyView() {
Log.i(TAG, getClass().getSimpleName() + ":onDestroyView()");
super.onDestroyView();
}
@Override
public void onDetach() {
Log.i(TAG, getClass().getSimpleName() + ":onDetach()");
super.onDetach();
}
@Override
public void onPause() {
Log.i(TAG, getClass().getSimpleName() + ":onPause()");
super.onPause();
countDownTimer.cancel();
}
@Override
public void onResume() {
super.onResume();
Log.i(TAG, getClass().getSimpleName() + ":onResume()");
}
@Override
public void onStart() {
Log.i(TAG, getClass().getSimpleName() + ":onStart()");
super.onStart();
}
@Override
public void onStop() {
Log.i(TAG, getClass().getSimpleName() + ":onStop()");
super.onStop();
}
private void showQuestion(int no) {
//set question sentences
question_TV.setText(test.getQuestionSentenceByQNo(no));
//set answer sentences
answer1_RB.setText(test.getAnswerListByQNo(no).get(0).getASentence());
answer2_RB.setText(test.getAnswerListByQNo(no).get(1).getASentence());
answer3_RB.setText(test.getAnswerListByQNo(no).get(2).getASentence());
answer4_RB.setText(test.getAnswerListByQNo(no).get(3).getASentence());
..
....
}
旋转设备后,我可以看到日志“设置问题!!!”和“问题号码”正确但视图无法通过showQuestion(test.getCurrentQuestionNo())更新;首先运行的方法。
答案 0 :(得分:0)
我理解我片段中的错误。 它在onCreateView中。当片段中有相同的视图时,首先需要创建一个视图,用于对应的布局和使用后的布局。 所以这是错误的方式:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.test_fragment, container, false);
}
&#13;
这是正确的方法:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.test_fragment, container, false);
// initialize here some view
return v;
}
&#13;