I'm trying to go back to my previous fragment
so I tried using onBackPressed()
and here is my code
@Override
public void onBackPressed()
{
if(getFragmentManager().getBackStackEntryCount() > 0)
getFragmentManager().popBackStack();
else
super.onBackPressed();
}
I'm having an error in super.onBackPressed()
and it says that "Cannot resolve method 'onBackPressed' so I'm wondering why so I've searched on how to go to my previous fragment and found out that I should used addToBackStack()
if (fragment != null) {
android.app.FragmentManager fragmentManager = ((Activity) context).getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.frame_container_expandable, fragment);
fragmentTransaction.commit();
} else {
Log.e(TAG, "Error in creating fragment");
}
when I'm trying to put addToBackStack() is doesn't recognized it. Why is it like that? Is it because I'm using android.app.FragmentManager
? or there are other solution for this?going back to the previous fragment? Thank you for the help
In my logcat it says
Error:(88, 18) error: cannot find symbol method onBackPressed()