Home > Mobile >  Fragment makes App exits on back press instead of loading parent activity
Fragment makes App exits on back press instead of loading parent activity

Time:08-16

I'm dealing with a scenario where I have an activity named HomeActivity which contains BottomAppBar and inside that activity I'm loading fragments when user clicks on icons placed in bottom app bar. Now the issue that I'm facing is that when I load fragment from home activity it opens but on back press it exits the app instead of reloading home activity layout. Instead of that I want to load home activity whenever back is pressed inside any loaded fragment code snippet attached below:

bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @SuppressLint("NonConstantResourceId")
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {

                switch (item.getItemId()) {

                    case R.id.homePage:
                         fragment = new Fragment1();
                        break;

                    case R.id.wallet:
                        fragment = new Fragment2();
                        break;

                    case R.id.myorders:
                        fragment = new Fragment3();
                        break;

                    case R.id.search:
                         fragment = new Fragment4();
                        break;
                }
                getSupportFragmentManager().beginTransaction().replace(R.id.rl_mainLayout, fragment).commit();
                return true;
            }
        });

Moreover, i have an override method of onBackPressed() which contains call to method finishAffinity()

@Override
    public void onBackPressed() {
        finishAffinity();
    }

CodePudding user response:

just don't call finishAffinity() always, it is breaking your navigation flow... you should just call super in there for proper behavior

@Override
public void onBackPressed() {
    super.onBackPressed();
}

and finishAffinity() is just closing all activities. the real question is why you have intentional finishAffinity() call in onBackPressed and why you wondering it is working (not switching fragments, just closing HomeActivity and others if on activitys stack in background)

if you really need finishAffinity() call you probably should make it only when first Fragment is visible

@Override
public void onBackPressed() {
    if (isFirstFragmentVisible()) {
        finishAffinity();
        return; // prevent super call, handled
    }
    super.onBackPressed();
}

isFirstFragmentVisible() implement by own as homework, probably easiest way would be to check in this method which Fragment is currently loaded into R.id.rl_mainLayout (useful might be findFragmentById() or findFragmentByTag())

CodePudding user response:

According to what is assume you are trying to do is. you have 4 fragments in home activity. suppose user click on fragment 2, and then if he click back then fragment 1 should be displayed rather than splash or app exit. For this use the code below.

@Override
public void onBackPressed() {
    if (currentFragment instanceOf Fragment1) {
       super.onBackPressed();
    }
    else
   {  getSupportFragmentManager().beginTransaction().replace(R.id.rl_mainLayout, 
      fragment1).commit();
   }
    
}

set currentFragment value equal to fragment1 by default and then change its value on onNavigationItemSelected accordingly.

  • Related