保存和还原动态编辑的片段的状态

时间:2015-11-19 20:41:48

标签: android-fragments savestate

我有一个按钮,可以在我的片段中添加textview。当我关闭活动时,textview消失。我唯一想知道的是如何保存和检索创建的文本视图。

public class trainingsplan extends Fragment {
ImageButton Btn;
LinearLayout Layout;
private ArrayList<View> mViews= new ArrayList<View>();
int i;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.trainingsplan, container, false);
    setHasOptionsMenu(true);

    Btn = (ImageButton) rootView.findViewById(R.id.imageButton3);
    Layout = (LinearLayout) rootView.findViewById(R.id.linearLayout);
    LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
    final TextView Test = new TextView(getActivity());
    Test.setLayoutParams(lparams);
    Test.setText("Hallo wie gehts dir ?");
    Btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Layout.addView(Test);
            mViews.add(Test);
        }
    });

    return rootView;

}

0 个答案:

没有答案