繁体   English   中英

OnCreateView Fragment调用了两次

[英]OnCreateView Fragment called twice

我有这个问题,当用户旋转设备时,我的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;
}

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

android:configChanges="orientation"

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

android:configChanges="orientation|screenSize"

清单中的活动片段

资源

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM