In Fragment on Back Button Pressed Activity Is Blank

In Fragment on back button pressed Activity is blank

If you have more than one fragment been used in the activity or even if you have only one fragment then the first fragment should not have addToBackStack defined. Since this allows back navigation and prior to this fragment the empty activity layout will be displayed.

 // fragmentTransaction.addToBackStack() // dont include this for your first fragment.

But for the other fragment you need to have this defined otherwise the back will not navigate to earlier screen (fragment) instead the application might shutdown.

Blank screen is displayed when pressing the back button to move to previous fragment

You need to change if condition as per below

if(fragmentManager.getBackStackEntryCount() - 1 == 0){
//Do your code
}else{
fragmentManager.popBackStack();
}

As per your code please find below full code

public void onBackPressed() {
final FragmentManager fragmentManager = getSupportFragmentManager();
if (drawerLayout.isDrawerOpen(GravityCompat.START)) {
drawerLayout.closeDrawer(GravityCompat.START);
} else {
if (fragmentManager.getBackStackEntryCount() - 1 == 0) {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AlertDialogCustom);
builder.setTitle("Sign out");
builder.setMessage("Are you sure you want to sign out?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
firebaseAuth.signOut();
dialog.dismiss();
startActivity(new Intent(getApplicationContext(), WelcomeActivity.class));
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
fragmentManager.popBackStack();
}
}
}

I hope this can help You!

Thank You.

Pressing back on last fragment shows blank Activity shortly before going to previous Activity

Ok, after fighting with the backStack for many days, I decided to skip it, since it does not seem to be working properly, or I'm not understanding it.

The way I did it is on my Activity's onBackPressed i'm checking which fragment is showing and if it's the one that should be the last one (Multiple ones are the "last one") I just finish() the Activity.

@Override
public void onBackPressed() {

MyFragment myFragment = (MyFragment)fragmentManager.findFragmentByTag("MyFragment");
if (myFragment != null && myFragment .isVisible()) {
//if myFragment is visible when the back button is pressed...
finish();
} else {
super.onBackPressed();
}
}

Don't forget to give a name to your fragment in the FragmentManager transaciton. In my case it is "MyFragment".

Previous fragment shows blank screen when back button pressed (using multiple fragmentTransactions)

The solution was to put the fun goToSecondFragment() inside of override fun onViewCreated

Fragment overlap after back button pressed

I tried using @ADM duplicate link, it would take me to the fragment A but if I loaded fragment B for a second time I would get double commit errors.

The solution that works for me is to restart the activity with the @Override onBackPressed(), this will reload the fragmentA by default.

Fragment B

FragmentManger fm = fragmentManager();
FragmentTransaction ft = fm.beginTransaction();
fm.replace(R.id.container, fragmentC, "fragmentC");
.commit();

fragmentC backbutton pressed

Activity

@Override
public void onBackPressed() {
if(getSupportFragmnetManager().findFragmnetByTag("fragmentC") !=null) {
Intent myIntent = new Intent(this, Activity.class);
startActivity(myIntent);
finish();
} else {
super.onBackPressed();
}
}

This solution will only work if you want to go back to the first fragment loaded by the Activity, so it may not be the best solution out there but it worked for me.

OnBack Press Fragment showing blank Screen

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 .



Related Topics



Leave a reply



Submit