I have some code in the onCreate method an Activity and noticed that it is being called three times. Is it normal behaviour? Thanks.
I have some code in the onCreate method an Activity and noticed that it is being called three times. Is it normal behaviour? Thanks.
In my case, onCreate method of the subclass is running twice. Changing theme after onCreate method of the superclass is called is causing this. I set theme before onCreate method of superclass then onCreate method of subclass was not called again.
public class XActivity extends YActivity { // XActivity is subclass
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xl);
}
...
public class YActivity extends AppCompatActivity { //YActivity is superclass.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTheme();
}
...
Converted to this:
public class YActivity extends AppCompatActivity { // YActivity is superclass.
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
setTheme();
super.onCreate(savedInstanceState);
}
...
I had a similar problem, it was caused by MobileAds.
After I initialized them BEFORE super.onCreate(...) the problem was gone.
This can also occur if you have in Developer Settings "Don't Leave Activities" turned on.
In Some cases it might be because of logging multiple times. Run your application in debugging mode and check if your code runs twice or its just logging multiple times.
If its just logging check my answer in this question: Logcat showing information 3 times on AVD
I just had this issue and after reading all this, nothing helped. Here is what helped me.
MainLauncher = true to your MainActivity.cs class.In my case it was calling setDefaultNightMode after onCreate:
super.onCreate(savedInstanceState);
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
this fixes it:
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
super.onCreate(savedInstanceState);