android


onCreate() not being called on orientation change


I have an MainActivty which consists of a Navigation Drawer. The navigation drawer code is pretty close to the official documentation. Now the MainActivty loads FragmentA upon start. The Navigation Drawer is supposed to be only available from FragmentA. So far so good, all works fine.
Now I tap something on the screen, which the MainActivity recognizes, and replaces FragmentA by FragmentB. When I am on FragmentB, in the FragmentB.onCreate() I call the following:
setHasOptionsMenu(true);
((MainActivity)getActivity()).mDrawerToggle.setDrawerIndicatorEnabled(false); //ActionBarDrawerToggle mDrawerToggle is a public class variable in MainActivity
This is in order to replace the Drawer carat in the actionbar with a Back arrow. This works fine as well.
Now, at this point, if I change the screen orientation the MainActivty.onCreate() is not called. The FragmentB.onCreate() is still called, and the mDrawerToggle is null as the MainActivity.onCreate() was not called. So the app crashes due to a NullPointerException.
I am NOT using: android:configChanges="orientation" for the activity. I am not sure how to fix this, as the example code in Android documentation has only a single Activity and no other fragments. Any help is appreciated.
Found the solution. It was a silly mistake. The Fragments onCreate() is called before the Activity's onCreate(). So I just put the code referring to the Activity in the Fragment.onCreateView(). Now all is well :)

Related Links

AsyncTask error from getView using a HashMap - Android
Can font-size be set to a percentage of page-width?
weakreferences of fragments on Orientation Change using FragmentStatePagerAdapter -Android
Eclipse Syntax error, insert “}” to complete classbody
How to override the onbackpressed event for Dialog box in android? [duplicate]
Can't import existing android projects into eclipse..
How to support PDF viewing for all mobile browser?
Reuse same bunch of statements several times
Is it possible to remotely host a resource used in a Android app in such a way that it can only be used by my app?
How to Preserve aspect ratio of a button's background image with xml
Android ListView background color change
Bootstrap on Phonegap cannot be responsive
youtube video in android without hardware acceleration
Android emulator x86 HAXM GPU Windows 7 crash BSOD
Android: Stopping MediaPlayer activity of widget during Phone Calls
Why a socket server on android always listen on IPv6?

Categories

HOME
twitter-bootstrap-3
webdriver
pda
apache-spark-mllib
jwplayer
socket.io
google-cloud-functions
parameters
nested-if
character
elasticsearch-5
arangodb
owin
reactive-programming
speech-synthesis
docx
microsoft-dynamics-nav
bourbon
windows-xp
poedit
parcelable
monit
itunes
mat
ms-access-2003
inversion-of-control
rpgle
comparable
failover
userdefaults
panoramas
turfjs
r-grid
nested-lists
least-squares
batching
engine.io
protege4
apiary.io
apic
docfx
jpad
z-notation
chef-solo
signals-slots
naming
placeholder
xmldocument
flask-login
procfile
ovf
globalize
check-mk
flyout
django-1.10
testrail
seek
signalr.client
model-associations
wso2developerstudio
ng-grid
installshield-2010
dup
android-dateutils
eigenvector
angular-ui-typeahead
exim4
user-management
sapi
ons-api
toolkit
zscript
git-ftp
delayed-execution
primitive-types
deform
insight.database
build-script
tinyxml
xmltodict
embedded-fonts
away3d
dtmf
os.system
dajaxice
iboutlet
extconf.rb
oracle-enterprise-linux
entity-attribute-value
user-tracking
google-floodlight
drawtobitmap
creole
mbeans
w3c-geolocation
metaweblog
non-clustered-index
django-piston
dsl-tools
technical-debt

Resources

Encrypt Message