'Can onCreate method be called multiple times when there is no configuration changes?
onCreate() method for one of my Activities is being called multiple times and consistently about once every second. I checked the documentation and it says that configuration changes may cause such behavior. I checked for configuration changes using the following code:
on manifest file I added:
android:configChanges="screenLayout|touchscreen|mnc|mcc|density|uiMode|fontScale|orientation|keyboard|layoutDirection|locale|navigation|smallestScreenSize|keyboardHidden|colorMode|screenSize"
On the activity I added:
@Override
public void onConfigurationChanged(@NotNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.i(TAG, "onConfigurationChanged: "+newConfig);
}
The method onConfigurationChanged is never called which gave me the impression that configuration is never changed. I am not sure what's causing the "onCreate" and "onDestroy" calls. What could be the possible reasons?
[Sorry for not sharing the rest of the code. I am looking for some generic answers on what could cause the onCreate and onDestroy methods be called except for explicit user interaction]
Edit:
Activity is being started only once and I am not explicitly calling onCreate anywhere in the code.
public static void startActivity(Activity activity, Intent intent, int...anim) {
Log.i(TAG, "startActivity: activity started"); //prints only once
if (intent == null) {
intent = new Intent();
}
intent.setClass(activity, <My Activity Name>.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(intent);
if (anim != null && anim.length > 1) {
activity.overridePendingTransition(anim[0], anim[1]);
}
activity.finish();
}
Solution 1:[1]
Can onCreate method be called multiple times when there is no configuration changes?
Only if your activity is being started repeatedly, or you are somehow trying to call onCreate() yourself.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | CommonsWare |
