9

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

Pooja Dubey
  • 683
  • 2
  • 14
  • 34

7 Answers7

18

My suggestion for you is that let addToBackStack(null) in the fragment call.

But remove this line addToBackStack(null)

from the Homepage after which onbackpress you are getting the blank screen hope this will for you .

Developer
  • 6,292
  • 19
  • 55
  • 115
5

Fragment showing blank solutions.

  1. Make sure to add first Base Fragment without addToBackStack() method. Because it is responsible for Navigation Back. We don't want to go back from first Page. So don't add it to 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. So in order to hit onResume or onViewCreated while going back to Fragment A. you should call fragmentTransaction.replace
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300
2
@Override
public void onBackPressed() {
    int fragments = getSupportFragmentManager().getBackStackEntryCount();
    if (fragments == 1) {
        finish();
    } else {
        if (getFragmentManager().getBackStackEntryCount() > 1) {
            getFragmentManager().popBackStack();
        } else {
            super.onBackPressed();
        }
    }
}
Pablo Cegarra
  • 20,955
  • 12
  • 92
  • 110
1

addToBackStack() <--dont include this for your first fragment.-->

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

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,

 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

Community
  • 1
  • 1
Spring Breaker
  • 8,233
  • 3
  • 36
  • 60
  • same issue it is showing a blank screen then it is closing the application – Pooja Dubey Mar 06 '14 at 10:47
  • @PoojaDubey: Well I guess there is some other problem in your code, because the above way works.You can check the sample from here https://www.dropbox.com/s/iup67qfwkx5uzqo/FragmentsTest.rar – Spring Breaker Mar 06 '14 at 11:05
0

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.

Manasvi
  • 450
  • 7
  • 9
0

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!!

Calyfs0
  • 111
  • 1
  • 11