My issue is quite simple. I'm using fragments over an activity with a tablayout (but not a viewpager). All my fragments inherit from a BaseFragment class, which has this function:
public void pushFragmentAnimated(BaseFragment fragment) {
mActivity.pushFragmentAnimated(fragment);
}
In the activity, pushFragmentAnimated()
is:
public void pushFragmentAnimated(BaseFragment fragment) {
try {
mCurrentFragment = fragment;
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_left, R.anim.enter_from_left, R.anim.exit_to_right);
transaction.replace(R.id.fragment_container, fragment, fragment.TAG);
transaction.addToBackStack(fragment.getFragmentTitle());
transaction.commit();
manager.executePendingTransactions();
} catch (Exception e) {
e.printStackTrace();
}
}
Problem is, using this function, i never get my fragment state saved, and whenever i pop my current fragment, the fragment below reloads as a new one.
In fact, in my BaseFragment i have two functions, which i usually override in every fragment:
public void initializeFragment() {}
public void setupFragmentData() {}
Those functions are called here:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = getView();
if (view == null && savedInstanceState == null) {
int layoutResource = getFragmentLayout();
view = inflater.inflate(layoutResource, container, false);
// ButterKnife bind
ButterKnife.bind(this, view);
// Initialization methods
initializeFragment();
}
return view;
}
and here:
@Override
public void onResume() {
super.onResume();
// Initialization methods
setupFragmentData();
}
and the first one, initializeFragment()
, always gets called when i pop a fragment, even though it shouldn't. Only the second should, as you can see.
Any idea?