BackStack片段轮换空白 [英] BackStack Fragments blank on rotation

查看:194
本文介绍了BackStack片段轮换空白的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的活动,我装碎片,将它们添加到backstack作为用户点击的项目。这工作正常和backstack负荷很好。

In my Activity, I am loading fragments and adding them to the backstack as the user clicks on items. This works fine and the backstack loads well.

不幸的是,当我旋转设备,该活动是空白的。我曾尝试一切可能的SO和谷歌。

Unfortunately, when I rotate the device, the activity is blank. I have tried everything possible on SO and on Google.

我试图通过简单地替换片段作为用户点击和处理后的管理资产净值导航自己。这工作得很好,但我失去了我coded切换抽屉式导航栏显示后退箭头堆栈是大于1的自动功能。

I tried managing the navigation myself by simply replacing the fragments as the user clicks and handling the back as nav. This works fine but I lose the automatic ability which I coded to change the navigation drawer to show the back arrow if the stack is greater than 1.

我试图消除整个堆栈和在当装置旋转时,用户点击顺序加入个别片段。这不能很好地工作。似乎拆除和更换都出来了怪人。

I tried removing the entire stack and adding the individual fragments in the order the user clicks when the device is rotated. This doesn't work well. Seems remove and replace are out of wack.

我尝试添加片段回用fragmentManager.findbytag但它抛出非法状态异常已添加片段。我发现SO解决除去过渡。一次得手后停止。不知道为什么。

I tried adding the fragment back using fragmentManager.findbytag but it throws an illegal state exception "fragment already added". I found a solution on SO to remove the transition. Worked once then stopped. No idea why.

我把举杯检查显示backstack每次改变时间算,我意识到,栈仍然显示正确的计数增加后,但在网页仍是空白。它甚至需要2回presses到活动中退出到previous活动。

I put in a toast to check show the backstack count each time it changes, and I realized that the stack still shows the correct count after the addition but the page is still blank. It even requires 2 backpresses to exit the activity to the previous activity.

我把一个断点,以检查是否onCreateView被称为片段,是的,它被称为每个手机旋转的时间。

I put a breakpoint to check if onCreateView is called in the fragment and yes, it is called each time the phone is rotated.

基本上,我的code现在看起来是这样的,因为我已经放弃了。

Basically, my code now looks like this since I have given up.

@Override
    protected void onCreate(Bundle savedInstanceState) {
        mPosition = 1;

        super.onCreate(savedInstanceState);

        if(savedInstanceState != null) {
            mLastArtistFragmentTag = savedInstanceState.getString(SAVE_FRAGMENT_LAST_ARTIST);
            mLastAlbumFragmentTag = savedInstanceState.getString(SAVE_FRAGMENT_LAST_ALBUM);
            mLastSongFragmentTag = savedInstanceState.getString(SAVE_FRAGMENT_LAST_SONG);

            mLastFragmentType = savedInstanceState.getInt(SAVE_FRAGMENT_LAST_TYPE);
        }

        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Load();
            }
        }, 100);
    }

    void Load() {

        findViewById(R.id.loading).setVisibility(View.GONE);

        ViewStub stub = (ViewStub) findViewById(R.id.container_stub);
        stub.setLayoutResource(R.layout.activity_music);
        stub.inflate();




    if(mLastArtistFragmentTag == null) {
        getSupportFragmentManager().beginTransaction()
                .replace(R.id.base_container, new Artists(), FRAGMENT_TAG_BASE)


                   .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE)
                    .commit();
        }
            else {
                getSupportFragmentManager().beginTransaction()
                        .replace(R.id.base_container, new Artists(), FRAGMENT_TAG_BASE)
                    //.add(R.id.base_container, Albums.newInstance(mLastArtistFragmentTag), mLastArtistFragmentTag)
//.add(R.id.base_container, getSupportFragmentManager().findFragmentByTag(mLastArtistFragmentTag), mLastArtistFragmentTag)
                    //.addToBackStack(mLastArtistFragmentTag)
                .setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
                .commit();
        }

    }

@Override
    public void onSaveInstanceState(Bundle instanceState) {
        super.onSaveInstanceState(instanceState);

        if(mLastFragmentType != -1) {
            instanceState.putString(SAVE_FRAGMENT_LAST_ARTIST, mLastArtistFragmentTag);
            instanceState.putString(SAVE_FRAGMENT_LAST_ALBUM, mLastAlbumFragmentTag);
            instanceState.putString(SAVE_FRAGMENT_LAST_SONG, mLastSongFragmentTag);

            instanceState.putInt(SAVE_FRAGMENT_LAST_TYPE, mLastFragmentType);
        }

    }

片段code是这样

Fragment Code is like this

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

    final ListView listView = (ListView) inflater.inflate(R.layout.fragment_albums, container, false);
    listView.addHeaderView(inflater.inflate(R.layout.header_artist_info, null));

    try {
        String uri2 = "http://someurl";
        NetworkRequest.FetchJSON(Request.Method.GET, uri2, new Response.Listener<JSONObject>() {
            @Override
            public void onResponse(JSONObject response) {
                try {
                    JSONArray songs = response.getJSONArray("songs");
                    //HashMap<String, Song> recentSongs = new HashMap<String, Song>();
                    ArrayList<Song> latest = new ArrayList<Song>();

                    for (int i = 0; i < songs.length(); i++) {
                        Song s = new Song(songs.getJSONObject(i));
                        latest.add(s);
                    }

                    AlbumListAdapter pgiAdapter = new AlbumListAdapter(getActivity(), latest);
                    SwingBottomInAnimationAdapter swingBottomInAnimationAdapter = new SwingBottomInAnimationAdapter(pgiAdapter);

                    swingBottomInAnimationAdapter.setAbsListView(listView);
                    listView.setAdapter(swingBottomInAnimationAdapter);

                } catch (JSONException ex) {
                    Toast.makeText(getActivity(), ex.getMessage(), Toast.LENGTH_LONG).show();
                }
            }
        });
    } catch (Exception ex) {
        Toast.makeText(getActivity(), ex.getMessage(), Toast.LENGTH_LONG).show();
    }

    return listView;
}

你问之前,是我没有移动Load方法onCreate方法中。仍然没有工作。

before you ask, yes I did move the Load method to inside the onCreate method. Still did not work.

我真的坚持在这里,需要你的帮助。请帮帮我。谢谢你。

I am really stuck here and need your help. Please help me. Thanks.

推荐答案

取决于你如何重建可能需要手动处理片段状态变化的活动。在最坏的情况下,您可能需要断开并重新片段强制视图娱乐。

Depending on how you recreate the activity you may need to manually handle fragment state changes. In the worst of cases you may need to detach and reattach the fragment to force view recreation.

public void onConfigurationChanged(Configuration newConfig) {
  super.onConfigurationChanged(newConfig);
  FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
  transaction.detach(activeFrag).attach(activeFrag).commit();
}

和拥有正确的钩子上的Andr​​oidManifest.xml

and have the correct hook on your AndroidManifest.xml

android:configChanges="orientation|screenSize"

这篇关于BackStack片段轮换空白的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆