Annie Annie - 11 days ago 7
Android Question

Fragment after fragment in android

I have a question that whenever i open a new fragment from another fragment it open up but when i come back by back button then why does it holds the background view of the previous fragment, if i add this piece of code to that fragment if

(container != null) {
container.removeAllViews();
}


then it gets out on whole screen where i want it to be under the toolbar, it gets out of it and toolbar is not shown and sometimes app stops also.
I am a newbie to fragments so please answer my question in a calm and detailed way.
I have added a flow of how screens are appearing and this are fragments except first one,when it comes to 2nd again that time the screen looks partially like another.this is the flow of all the screensthis is the screen when flow goes from fourth to second fragment, partial scrren visibility problem as i want to show the background fully only and not the table layout onewhich is appearing over it

I use this code to add fragments on click of buttons

Addhearingnewfrag fragment = new Addhearingnewfrag();
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.contentFragment, fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});

Answer

I guess the problem beacuse you are using wrong fragment manager. If you open fragment from another fragment you should use

getChildFragmentManager()

instead of

getActivity().getSupportFragmentManager()

UPDATE

public class MyActivity implements Activity implements ChangeFragListener{

public interface ChangeFragListener{
     change(String id);
}            

@Override
public void change(String id){
  ...
  if(id.equals("addxx")){
            Addhearingnewfrag fragment = new Addhearingnewfrag();
            FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            fragmentTransaction.replace(R.id.contentFragment, fragment);
            fragmentTransaction.addToBackStack(null);
            fragmentTransaction.commit();
        }
    });
  }
}

}

XFRagment.java

public XFragment extends Fragment{
   private ChangeFragListener listener;

   protected void onCreate(Bundle savedInstanceState) {
        listener = (ChangeFragListener)getActivity();
   }

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

btn.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
       listener.change("fromOtherId");
    }
});

    }
}

You can use this interface in other fragments too. You shouldn't directly replace fragment from other fragment unless One is another's child.

UPDATE - 2

After review the project the main problem is because of fragment container.For example; you add home fragment to R.id.frame. But you replace other fragments to R.id.drawer_layout. And R.id.drawer_layout is reference to your Navigation Drawer not fragment container.

Moreover, R.id.frame does not looking like as fragment container. It has view components. Here there are sample navigation drawer examples with fragments