片段动态布局保存ui更改屏幕方向

时间:2017-11-14 15:36:54

标签: android android-fragments state

我正在尝试维护ui更改,以便在重新启动活动时,我的片段实例将随按钮的更改一起使用。所以我发现setRetainInstance(true)并没有破坏片段对象,但是,结果视图被破坏了。因此,我在片段类中覆盖了onSaveInstance,以保存在事件处理程序中调用的资源的ID,以便在下次创建活动时对其进行设置。结果出人意料,至少对我来说: 1.在重新创建活动时会破坏片段的视图,并且片段实例将重新附加到新的活动实例。 2.事件处理程序变得无法运行,因此我不知道问题出在哪里。

以下是活动类的代码:

package com.voicenoteinc.ticatactoy;

import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.PersistableBundle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

    public class MainActivity extends AppCompatActivity {
        Tic_Fragment s;
        FragmentManager manager;
        String TAG_FRAGMENT = "tag_fragment";
        FragmentTransaction trans;
        boolean retained = true;
        String COLOR_ARRAY;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            manager = getFragmentManager();
            s = (Tic_Fragment) manager.findFragmentByTag(TAG_FRAGMENT);
            if(s == null){
                s = new Tic_Fragment();
                retained = false;
                trans = manager.beginTransaction();
                trans.add(R.id.fragment_container,s,TAG_FRAGMENT).commit();
            }
        }
    }

以下是Fragment类的代码:

package com.voicenoteinc.ticatactoy;

import android.app.Fragment;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Switch;

public class Tic_Fragment extends Fragment {
    public Button bu1,bu2,bu3,bu4,bu5,bu6,bu7,bu8,bu9;
    int COLOR[] = new int[9];
    public String COLOR_KEY = "COLOR_KEY";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        for(int i =0; i<9;i++){
            COLOR[i] = R.color.granite;
        }
       return inflater.inflate(R.layout.tic_fragment,container,false);

    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        for(int i =0; i<9;i++){
            COLOR[i] = R.color.granite;
        }
        super.onViewCreated(view, savedInstanceState);
        if(savedInstanceState!=null){
            COLOR=savedInstanceState.getIntArray(COLOR_KEY);
            bu1.setBackgroundResource(COLOR[0]);
            bu2.setBackgroundResource(COLOR[1]);
            bu3.setBackgroundResource(COLOR[2]);
            bu4.setBackgroundResource(COLOR[3]);
            bu5.setBackgroundResource(COLOR[4]);
            bu6.setBackgroundResource(COLOR[5]);
            bu7.setBackgroundResource(COLOR[6]);
            bu8.setBackgroundResource(COLOR[7]);
            bu9.setBackgroundResource(COLOR[8]);
        }else{
            bu1 = getView().findViewById(R.id.bu1);
            bu2 = getView().findViewById(R.id.bu2);
            bu3 = getView().findViewById(R.id.bu3);
            bu4 = getView().findViewById(R.id.bu4);
            bu5 = getView().findViewById(R.id.bu5);
            bu6 = getView().findViewById(R.id.bu6);
            bu7 = getView().findViewById(R.id.bu7);
            bu8 = getView().findViewById(R.id.bu8);
            bu9 = getView().findViewById(R.id.bu9);
        }
        eventhandlers();
    }

    public void eventhandlers(){
        bu1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu1,1);
            }
        });
        bu2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu2,2);
            }
        });
        bu3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu3,3);
            }
        });
        bu4.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu4,4);
            }
        });
        bu5.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu5,5);
            }
        });
        bu6.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu6,6);
            }
        });
        bu7.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu7,7);
            }
        });
        bu8.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu8,8);
            }
        });
        bu9.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                display(bu9,9);
            }
        });

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putIntArray(COLOR_KEY,COLOR);
    }
    public void display(Button bu, int i){
        int backgroundColor = R.color.greenery;
        bu.setBackgroundResource(backgroundColor);
    COLOR[i-1] = backgroundColor;
}
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setRetainInstance(true);
    }
}

感谢ou的帮助,以及我是新手使用碎片和动态布局的方式

0 个答案:

没有答案