Android: Getting white screen with fragment transaction

守給你的承諾、 提交于 2019-12-05 08:32:54

replace fragment as:

    Fragment feeddetailFragment = new FeedDetailFragment();
feedFragment.getFragmentManager().beginTransaction()
.replace(R.id.frame_container, feeddetailFragment,Constant.FRAGMENT_FEEDDETAIL)
.addToBackStack(null)
.commit();

It will work..

If Still not working then try to check in the fragment which you want to show after replacement whether that fragment's view is null or not

if (view != null) {
        ViewGroup parent = (ViewGroup) view.getParent();
        if (parent != null)
            parent.removeView(view);
    }
    try {

        view = inflater.inflate(R.layout.fragment_business_details,
                container, false);

        initializeViewObjects();
        setListeners();

    } catch (InflateException e) {

        return view;
    }

    return view;
}

One obvious thing that sticks out to me is that you have ft.addToBackStack(null); twice. Pretty sure it should only be in there once.

Additionally, I believe you need to call fm.executePendingTransactions() directly after ft.commit().

I solved the problem by removing the support for v4 of fragments since that is only required by my TabAdapter but only for the fragments that are represented in tabs like news,vide,etc. From the fragments that aren't managed as tabs such as NewsContent.java

here is how my method to open another fragment from Tab1 should've been.

public void openNewsCont() {

    NewsContent nc = new NewsContent();
    FragmentManager fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();
    ft.replace(R.id.container, nc);
    ft.addToBackStack(null);
    ft.commit();
 }

And here is how the NewsContent should be:

package com.example.link_test;

    import android.app.Fragment;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;

    public class NewsContent extends Fragment {

        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        final View android = inflater.inflate(R.layout.news_content, container, false);


        return android;

        }
    }

Thanks to Eagle11 for his effort to help me with this nooby question that took me a whole morning to fix. I hope this answer helps another newbies like me.

you can have this code on the activity that hosts the fragments. It checks your stack and relaunches the fragment. That's one of the ways I decided to handle the issue on my end.

@Override
    public void onBackPressed() {
        int fragments = getFragmentManager().getBackStackEntryCount();
        Toast.makeText(this, String.valueOf(fragments), Toast.LENGTH_LONG).show();

        if (fragments == 0) {
            FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.container, new NewsContent());
            fragmentTransaction.commit();

            super.onBackPressed();
        }
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!