android – 活动和片段生命周期和方向变化

前端之家收集整理的这篇文章主要介绍了android – 活动和片段生命周期和方向变化前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我已经有非常奇怪的问题,片段和方向的变化已经导致强制关闭,而不是遵循逻辑模式.

我创建了一个简单的Activity和Fragment生命周期调试应用程序,它通过报告对logcat的调用来简单地实现Activity lifecycleFragment lifecycle的每一个步骤.

以下是TestActivity和TestFragment类:

测试活动

public class TestActivity extends Activity {
    Context ct = null;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("ACT","onCreate called");

        ct = getApplicationContext();

        FrameLayout fl = new FrameLayout(ct);
        fl.setId(1000);

        TestFragment tf = new TestFragment();
        getFragmentManager().beginTransaction().add(fl.getId(),tf,"").commit();

        setContentView(fl);
    }

    @Override
    protected void onStart() {
        Log.e("ACT","onStart called");
        super.onStart();
    }

    @Override
    protected void onResume() {
        Log.e("ACT","onResume called");
        super.onResume();
    }

    @Override
    protected void onPause() {
        Log.e("ACT","onPause called");
        super.onPause();
    }

    @Override
    protected void onStop() {
        Log.e("ACT","onStop called");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        Log.e("ACT","onDestroy called");
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        Log.e("ACT","onRestart called");
        super.onRestart();
    }
}

TestFragment

public class TestFragment extends Fragment {
    Context ctFrag = null;

    @Override
    public void onAttach(Activity activity) {
        Log.e("FRAG","onAttach called");
        super.onAttach(activity);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Log.e("FRAG","onCreate called");

    }

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        Log.e("FRAG","onCreateView called");

        ctFrag = ((TestActivity) getActivity()).ct;

        TextView tv = new TextView(ctFrag);
        tv.setText("My test TextView");

        return tv;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        Log.e("FRAG","onActivityCreated called");
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onStart() {
        Log.e("FRAG","onStart called");
        super.onStart();
    }

    @Override
    public void onResume() {
        Log.e("FRAG","onResume called");
        super.onResume();
    }

    @Override
    public void onPause() {
        Log.e("FRAG","onPause called");
        super.onPause();
    }

    @Override
    public void onStop() {
        Log.e("FRAG","onStop called");
        super.onStop();
    }

    @Override
    public void onDestroyView() {
        Log.e("FRAG","onDestroyView called");
        super.onDestroyView();
    }

    @Override
    public void onDestroy() {
        Log.e("FRAG","onDestroy called");
        super.onDestroy();
    }

    @Override
    public void onDetach() {
        Log.e("FRAG","onDetach called");
        super.onDetach();
    }
}

在初始化时,Logcat输出遵循预期顺序(启动活动,分片附加,生命周期调用发生等):

01-29 10:12:50.270: E/ACT(3321): onCreate called
01-29 10:12:50.760: E/FRAG(3321): onAttach called
01-29 10:12:50.760: E/FRAG(3321): onCreate called
01-29 10:12:50.760: E/FRAG(3321): onCreateView called
01-29 10:12:50.770: E/FRAG(3321): onActivityCreated called
01-29 10:12:50.770: E/ACT(3321): onStart called
01-29 10:12:50.770: E/FRAG(3321): onStart called
01-29 10:12:50.770: E/ACT(3321): onResume called
01-29 10:12:50.770: E/FRAG(3321): onResume called

但问题是,当一个方向改变发生时,Android Documentation说:

When such a change occurs,Android restarts the running Activity (onDestroy() is called,followed by onCreate())

这将表明它应该关闭活动及其内容,如生命周期建议(然后它)然后通过相同的有序过程重新创建活动在新的方向.这不会发生,似乎尝试重新创建片段,然后在活动娱乐中创建新的片段.

01-29 10:17:52.249: E/FRAG(3321): onPause called
01-29 10:17:52.259: E/ACT(3321): onPause called
01-29 10:17:52.269: E/FRAG(3321): onStop called
01-29 10:17:52.269: E/ACT(3321): onStop called
01-29 10:17:52.279: E/FRAG(3321): onDestroyView called
01-29 10:17:52.299: E/FRAG(3321): onDestroy called
01-29 10:17:52.299: E/FRAG(3321): onDetach called
01-29 10:17:52.299: E/ACT(3321): onDestroy called
01-29 10:17:52.650: E/FRAG(3321): onAttach called
01-29 10:17:52.650: E/FRAG(3321): onCreate called
01-29 10:17:52.650: E/ACT(3321): onCreate called
01-29 10:17:53.020: E/FRAG(3321): onCreateView called
01-29 10:17:53.020: E/FRAG(3321): onActivityCreated called
01-29 10:17:53.030: E/FRAG(3321): onAttach called
01-29 10:17:53.030: E/FRAG(3321): onCreate called
01-29 10:17:53.030: E/FRAG(3321): onCreateView called
01-29 10:17:53.030: E/FRAG(3321): onActivityCreated called
01-29 10:17:53.060: E/ACT(3321): onStart called
01-29 10:17:53.060: E/FRAG(3321): onStart called
01-29 10:17:53.060: E/FRAG(3321): onStart called
01-29 10:17:53.060: E/ACT(3321): onResume called
01-29 10:17:53.060: E/FRAG(3321): onResume called
01-29 10:17:53.060: E/FRAG(3321): onResume called

显然有很多解决方案可以解决这个问题,但是我的问题是为什么会发生这种情况?为什么一个片段引用被保留并重新创建,当它应该是该Activity的一部分,这被认为是完全被破坏和重新创建的?我可以通过故意将活动分开的碎片证明这一点.但是引发问题的原因是为什么在活动之前附加并重新创建了原始片段?它似乎并没有遵循Android进程的其余部分的逻辑生命周期.

解决方法

这是因为活动在销毁之前调用onSaveInstanceState(Bundle).默认情况下,该活动在此方法中保存其片段的状态.

稍后,当活动重新创建时,旧的片段将在活动onCreate(Bundle savedInstanceState)方法中重新创建.

您可能需要检查源代码herehere以更好地了解此行为.

猜你在找的Android相关文章