简体   繁体   English

onCreateView在Orientation Change之后重新创建片段后运行两次

[英]onCreateView running twice after recreation of fragment after Orientation Change

I have an Activity containing only a FrameLayout which displays a Fragment . 我有一个Activity只包含一个显示FragmentFrameLayout The Fragment only contains a Button . Fragment只包含一个Button

The working of the app is that the background color of the button changes on clicking the button. 该应用程序的工作是单击按钮时按钮的背景颜色发生变化。 The Fragment recieves the color value from an array defined in the MainActivity . Fragment从MainActivity定义的数组中获取颜色值。

The Activity's code is: 活动的代码是:

public class MainActivity extends AppCompatActivity {
    frag a;
    FragmentManager fm;
    static int color[] = {Color.RED,Color.BLUE,Color.GREEN};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        a = new frag();
        fm = getFragmentManager();
        FragmentTransaction ft = 
            (fm).beginTransaction().add(R.id.framelayout, a);
        ft.commit();
    }
}

And the Fragment code is: 片段代码是:

public class frag extends Fragment implements View.OnClickListener {

    View v;
    Button button;
    int i = 0;

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

        if (savedInstanceState!=null) {
            i = (int) savedInstanceState.get("i");
        }

        v = inflater.inflate(R.layout.fragment_frag, container, false);
        button = (Button) v.findViewById(R.id.button);
        button.setBackgroundColor(MainActivity.color[i]);
        button.setOnClickListener(this);
        return v;
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        outState.putInt("i",i);
    }

    @Override
    public void onClick(View vx) {
        i++;
        if(i%3==0)
            i=0;
        button.setBackgroundColor(MainActivity.color[i]);
    }
}

The problem is that when the screen is rotated and the fragment is recreated, the onCreateView runs twice one after another. 问题是当旋转屏幕并重新创建片段时, onCreateView一个接一个地运行。 The first time, it has a non-null savedInstanceState , but for the second time, the savedInstanceState becomes null (I've observed this using the debugger) . 第一次,它有一个non-null savedInstanceState ,但是第二次, savedInstanceState变为null (我已经使用调试器观察到了这一点)。 Hence, the color of the button always changes to the RED on re-creation of the fragment, which is the color at the first index of the color Array , and it seems as if the state of the app is not being saved at all. 因此,在重新创建片段时,按钮的颜色总是变为RED,这是color Array的第一个索引处的color Array ,并且看起来好像根本没有保存应用程序的state

Why is this happening? 为什么会这样?

onCreate in your Activity is called again when you rotate, which means another Fragment will be created and added to your frameLayout . 旋转时再次调用frameLayout Activity中的onCreate ,这意味着将创建另一个Fragment并将其添加到frameLayout The old one will be recreated, so you have two. 旧的将重建,所以你有两个。 Check the savedInstanceState in the Activity onCreate. 检查Activity onCreate中的savedInstanceState If it is not null then your Activity was recreated and it is likely that the Fragment is already there. 如果它不为null,则重新创建Activity,并且片段可能已存在。 You can also do fragmentManager.findFragmentById(id) and check if it's there before adding it again. 您还可以执行fragmentManager.findFragmentById(id)并在再次添加之前检查它是否存在。

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

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