OnBack Press Fragment showing blank Screen

2019-06-25 12:29发布

I am using Fragments in my Application.So on the home page i have a grid view of 5 item on selection of which again i am opening a new fragment .

So what is happening is on back press it is showing a blank screen after that it is closing the application .

Please suggest me what i have done wrong in this.

MyCode

gridView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View v, int arg2,
                    long arg3) {
                if (DASHBOARD_LINKS[arg2].equals("A")) {
                    fragTran.replace(R.id.content_frame,
                                firstFrag);
                        fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                        fragTran.addToBackStack(null);
                        fragTran.commit();

                }
                if (DASHBOARD_LINKS[arg2].equals("B")) {
                    fragTran.replace(R.id.content_frame, secondFrag);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack(null);
                    fragTran.commit();
                }
                if (DASHBOARD_LINKS[arg2].equals("C")) {
                    fragTran.replace(R.id.content_frame, thirdFarg);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack(null);
                    fragTran.commit();
                }
            }
        });

Please help my this is happening

7条回答
淡お忘
2楼-- · 2019-06-25 13:05

Update your OncreateView() method :

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    if (view != null) {
    if (view.getParent() != null) {
        ((ViewGroup)view.getParent()).removeView(view);
    }
    return view;
}

View v=inflater.inflate(R.layout.tests, container, false);

Hope this helps!!

查看更多
登录 后发表回答