I have a very simple Activity:
public class ActivityLogin extends FragmentActivity {
/**
*
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_login);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
FragmentManager fragmentManager = this.getSupportFragmentManager();
FragmentLogin fragment = (FragmentLogin) fragmentManager.findFragmentByTag(EnumFragmentTags.LOGIN.getValue());
if (fragment == null) {
Log.v("Activity", "Fragment is null");
fragment = new FragmentLogin();
fragmentManager.beginTransaction().add(R.id.login_layout, fragment, EnumFragmentTags.LOGIN.getValue()).commit();
} else {
Log.v("Activity", "Fragment is not null");
}
}
}
As you can see i only create my Fragment if it is null. The Fragment does not retain instance: So setRetainInstance(false). When i do some work in the background and call this.getResources() in my Fragment i always get:
Fragment not attached to Activity
after i rotated my device. If i dont rotate everything is fine. If i rotate i get the above error. If i use this.getActivity.getResources() instead i get:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.support.v4.app.FragmentActivity.getResources()'
So for some reason the activity does not get attached to the Fragment.
Why?
Do i have to use setRetainInstance(true) on my Fragment to get this working? But i thought doing this would lead into Memory issues...