0

I am selecting a default fragment in NavigationView when my activity starts up, it works alright but the problem with it is that when I press the back button, I see a blank view.

My Activity

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    private final String TAG = "MainActivity";
    NavigationView navigationView;


    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();
        String about_blog = "http://example.com/page/json/urls/p-about";
        String contri = "http://example.com/page/json/urls/contribute";
        String privy = "http://example.com/page/json/urls/privacy-rules";
        String contact = "http://example.com/page/json/urls/contact_us";
        String advert = "http://example.com/page/json/urls/advertise-here";
        String spons = "http://example.com/page/json/urls/sponsor-us";

        Fragment fragment = null;

        if (id == R.id.menu_home) {
            fragment = new PostFragment();
        }

        if (id == R.id.about_blog) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_contri) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_contact) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_privy) {
            fragment = new PageFragment();

        } else if (id == R.id.nav_advert) {
            fragment = new PageFragment();

        } else if (id == R.id.spons) {
            fragment = new PageFragment();

        } else if (id == R.id.app_about) {
            //fragment = new abtFragment();
        } else if (id == R.id.settings) {
            //fragment = new SettingFragment;
        }


        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.content_frame, fragment);
        ft.addToBackStack(null);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        ft.commit();

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Log.d(TAG, "Device rotated and onCreate called");

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        navigationView.setCheckedItem(R.id.menu_home);
        navigationView.getMenu().performIdentifierAction(R.id.menu_home, 0);
    }

To expatiate, when I launch the app, PostFragment is launched by default. When I click the back key, instead of the app closing, I meet a blank page.

Please, what am I doing wrong here and how do I correct it?

X09
  • 3,827
  • 10
  • 47
  • 92

1 Answers1

0

I have found the solution here. Just paste this

    FragmentManager fm = getSupportFragmentManager();
 fm.addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() { 

@Override public void onBackStackChanged() { if (getSupportFragmentManager().getBackStackEntryCount() == 0) finish(); } });

In the hosting activity's onCreate.

What this does is to finish the activity if the current fragment is the last in the back stack.

Community
  • 1
  • 1
X09
  • 3,827
  • 10
  • 47
  • 92