OnCreateView Fragment调用了两次

时间:2017-03-18 17:16:56

标签: android fragment

我有这个问题我的Fragment的OnCreateView方法在用户旋转设备时被调用两次并且它改变了第二次调用中的所有值,这里是这个片段的代码,我只是创建这个片段,如果saveInstanceState为先前的片段为空。

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



    // Inflate the layout for this fragment
    view =inflater.inflate(R.layout.fragment_datos_reborn, container, false);
    setRetainInstance(true);
    //declaracion de fragment de resultados

    if (getFragmentManager().findFragmentByTag("resultados_fragment")==null){
        resultadosFragment=new ResultadosFragment();
    }
    view.findViewById( R.id.aceptarDatos).setOnClickListener(this);
    //Asignaciones de RadioGroup del tipo de regresión
        mRGTipoDeRegresion=(RadioGroup)view.findViewById(R.id.rGTipoDeRegresion);

        rBTipoLineal=(RadioButton)view.findViewById(R.id.rBLineal);
        rBTipoLineal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tipoDeRegresion="lineal";
            }
        });
        rBTipoPotencial=(RadioButton)view.findViewById(R.id.rBPotecial);
        rBTipoPotencial.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tipoDeRegresion="potencial";
            }
        });
    //Se pregunta si es tablet
    esTablet=getResources().getBoolean(R.bool.es_tablet);

    //obtener recycler
    recycler = (RecyclerView)view.findViewById(R.id.recyclerDatos);
    recycler.setHasFixedSize(true);

    mBundle=this.getArguments();


    //        if (savedInstanceState==null){
    if (savedInstanceState==null){
        ejecutadoDesdePul=mBundle.getInt("ejecutado_desde_pulsasion");
    }else {
        ejecutadoDesdePul=0;
    }


    //        if (ejecutadoDesdePul==1){
    if (savedInstanceState==null){
        elementoFuePulsado=mBundle.getBoolean("elemento_pulsado");
        botonAceptarFuePulsado=mBundle.getBoolean("botonAceptarPulsado");
    }else {
        elementoFuePulsado=false;
        botonAceptarFuePulsado=false;
    }
    if(elementoFuePulsado==true){
        numeroDeVariables=0;
        variablesX=Funciones.convertirADoubleArrayComplejo(mBundle.getDoubleArray("valoresX"));
        variablesY=Funciones.convertirADoubleArrayComplejo(mBundle.getDoubleArray("valoresY"));
        elementoFuePulsado=false;
    }else if(botonAceptarFuePulsado==true) {
        numeroDeVariables=mBundle.getInt("numero_de_variables");
        botonAceptarFuePulsado=false;
        if (savedInstanceState==null){
            if (giroPantalla!=true) {
                variablesX = new Double[numeroDeVariables];
                variablesY = new Double[numeroDeVariables];
                giroPantalla=false;
            }
        }
    }
    tipoDeRegresion=mBundle.getString("tipo_de_regresion","");

    etiquetaX=mBundle.getString("nombre_x");
    etiquetaY=mBundle.getString("nombre_y");



    if (savedInstanceState==null) {
        poblarLista();
    }


    if (savedInstanceState==null){
        if (tipoDeRegresion.equals("lineal")){
            mRGTipoDeRegresion.check(R.id.rBLineal);
            Funciones.hacerToastCorto(getActivity(),"Entra");
        }else if (tipoDeRegresion.equals("potencial")){
            mRGTipoDeRegresion.check(R.id.rBPotecial);
            Funciones.hacerToastCorto(getActivity(),"Entra");
        }
        pasarAResultados();
    }
    if (ejecutadoDesdePul>=1){
        ejecutadoDesdePul++;
    }

    return view;
}

1 个答案:

答案 0 :(得分:1)

对于API 12及以下版本:添加

android:configChanges="orientation"

对于API 13或更高版本:添加

android:configChanges="orientation|screenSize"

到你的活动中,你的文章中有你的片段

source

相关问题