简体   繁体   English

OnBack Press Fragment显示空白屏幕

[英]OnBack Press Fragment showing blank Screen

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 . 我在我的应用程序中使用片段。在主页上,我有一个关于选择的5个项目的网格视图,我再次打开一个新片段。

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 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 请帮助我发生这种情况

My suggestion for you is that let addToBackStack(null) in the fragment call. 我的建议是让片段调用中的addToBackStack(null)

But remove this line addToBackStack(null) 但删除此行addToBackStack(null)

from the Homepage after which onbackpress you are getting the blank screen hope this will for you . 从主页上的onbackpress你得到空白屏幕希望这将为你。

Fragment showing blank solutions. 片段显示空白的解决方案。

  1. Make sure to add first Base Fragment without addToBackStack () method. 确保在没有addToBackStack ()方法的情况下添加第一个Base Fragment。 Because it is responsible for Navigation Back . 因为它负责导航返回 We don't want to go back from first Page. 我们不想从第一页回来。 So don't add it to backStack. 所以不要将它添加到backStack。
  2. While moving from Fragment A to Fragment B if you have call fragmentTransacation.add then while comming back from B to A. onViewCreated or onResume method will not called as you have added a fragment to it. 如果你有调用fragmentTransacation.add片段A移动到片段B ,那么当从B返回到A时, onViewCreatedonResume方法将不会调用,因为你已经添加了一个片段。 So in order to hit onResume or onViewCreated while going back to Fragment A. you should call fragmentTransaction.replace 因此,为了在返回Fragment A时点击onResumeonViewCreated ,你应该调用fragmentTransaction.replace
@Override
public void onBackPressed() {
    int fragments = getSupportFragmentManager().getBackStackEntryCount();
    if (fragments == 1) {
        finish();
    } else {
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
}

addToBackStack() <--dont include this for your first fragment.--> addToBackStack()< - 不要为你的第一个片段包含这个.-->

if(getSupportFragmentManager().getBackStackEntryCount() !=1){
                fragmentTransaction.addToBackStack("placeholder");
            }

This will happen if you navigating fragment from one activity to the fragment of the second activity. 如果您将片段从一个活动导航到第二个活动的片段,则会发生这种情况。 And while replacing your fragment you are adding them into backstack, so don't add the first fragment of respective activities into backstack. 在替换你的片段时,你将它们添加到backstack中,所以不要将相应活动的第一个片段添加到backstack中。

I am assuming that you want to handle back button press. 我假设您要处理后退按钮。 You can do the following, 你可以做到以下几点,

before transitioning between Fragments , call addToBackStack() like the following, Fragments之间转换之前,调用addToBackStack() ,如下所示,

 fragTran.replace(R.id.content_frame, secondFrag);
                    fragTran.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
                    fragTran.addToBackStack("TAG");
                    fragTran.commit();

For clear info please see this post https://stackoverflow.com/a/7992472/1665507 有关明确信息,请参阅此帖子https://stackoverflow.com/a/7992472/1665507

Update your OncreateView() method : 更新您的OncreateView()方法:

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!! 希望这可以帮助!!

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

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