0

I added a NavigationDrawer Activity in my Android project.

So I started adding 1 Fragment and it will be launched through one of the items in the NavigationDrawer.

My problem is that whenever I launch the Fragment, the content (in green) is not being changed. But contents of the new Fragment is there (in red). enter image description here

Here's my code in changing the fragment:

@Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        Fragment newFragment;
        android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        if (id == R.id.nav_gradesheet) {
            // Handle the camera action
            newFragment = new GradeSheet();
            transaction.replace(R.id.yeah2, newFragment);
            transaction.addToBackStack(null);
            transaction.commit();

        } else if (id == R.id.nav_summarygradesheet) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
Sagar Thakarar
  • 261
  • 3
  • 20
Jayson Tamayo
  • 2,741
  • 3
  • 49
  • 76

3 Answers3

0

You should replace

Fragment newFragment = new GradeSheet();

to:

GradeSheet gradeSheetFragment = GradeSheetFragment.newInstance();

and have your fragment set up like:

public static MultichoiceFragment newInstance(JSONObject questionObject) {

    Bundle args = new Bundle();
    args.putString(QUESTION_OBJECT, questionObject.toString());
    MultichoiceFragment fragment = new MultichoiceFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if(getArguments()!=null){
        try {
            joQuesstionObject = new JSONObject(getArguments().getString(QUESTION_OBJECT));
            BaseActivity.printOut("Question Object",joQuesstionObject.toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }

}
MichaelStoddart
  • 5,571
  • 4
  • 27
  • 49
0

Remove transaction.addToBackStack(null); from your code.

What's really happening?
By calling addToBackStack you tell android that you want the new Fragment UI to be on top of the old one, and since most of the new UI is transparent you can see you old fragment behind.

Nir Duan
  • 6,164
  • 4
  • 24
  • 38
  • Calling replace(); removes the fragment from the view and adds a new one to view, when replace is called onDestroyView is called inside the fragment destroying all of the fragments UI but saving the instance variables to the back stack – MichaelStoddart Aug 15 '16 at 07:58
0

the solution is that you should add the following line to the main fragment xml tag:

android:background="#ffffff"
android:clickable="true"

then in your java code:

@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    Fragment newFragment;
    android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

    if (id == R.id.nav_gradesheet) {
        // Handle the camera action
        newFragment = new GradeSheet();
        transaction.add(R.id.yeah2, newFragment);
        transaction.addToBackStack(null);
        transaction.commit();

    } else if (id == R.id.nav_summarygradesheet) {

    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}
Khalid Taha
  • 3,183
  • 5
  • 27
  • 43