I have an Activity
with a MapFragment
that I add to the Activity
programmatically using a FragmentTransaction
:
I have run a simple test:
public class MapFragmentOnBackStackExample extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map_fragment_on_back_stack_example);
FragmentManager fm = getSupportFragmentManager();
Fragment f = fm.findFragmentById(R.id.fragment_container);
if (f == null) {
f = SupportMapFragment.newInstance();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, f);
transaction.commit();
}
}
public void onAddFragmentClick(View view) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
transaction.replace(R.id.fragment_container, new MyFragment());
transaction.addToBackStack(null);
transaction.commit();
}
public static class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText("MyFragment: " + hashCode());
return textView;
}
}
}
and can't see any problems.
I could see the problem when commented if (f == null) {
leaving it to always create new fragment on rotation, which is obviously wrong, but that brings some suspicions.
Can you see more than 1 MapFragment in memory at the same time? Try using Eclipse Memory Analyzer (MAT).