Try this -
Manifest file -
(All 3 parameters are required to handle orientation change)
android:configChanges="keyboardHidden|orientation|screenSize">
Activity file -
boolean isFirstTimeAdLoading = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_rotate);
Log.e(TAG, "onCreate()");
if(isFirstTimeAdLoading)
{
// load ad
Log.e(TAG, "Ad Loaded for First Time");
// TODO : Call your Ad Loading Method here
// mark ad as 'already loaded once'
isFirstTimeAdLoading = false;
}
}
@Override
protected void onStart() {
super.onStart();
Log.e(TAG, "onStart()");
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
Log.e(TAG, "onConfigurationChanged()");
Log.e(TAG, "isFirstTimeAdLoading = " + isFirstTimeAdLoading);
}
Logs -
06-12 14:44:15.900 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onCreate()
06-12 14:44:15.900 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: Ad Loaded for First Time
06-12 14:44:15.900 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onStart()
06-12 14:44:24.320 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onConfigurationChanged()
06-12 14:44:24.320 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: isFirstTimeAdLoading = false
06-12 14:44:26.070 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onConfigurationChanged()
06-12 14:44:26.070 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: isFirstTimeAdLoading = false
06-12 14:44:27.880 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: onConfigurationChanged()
06-12 14:44:27.880 11541-11541/com.example.futech.stackoverflowsamples E/RotateActivity: isFirstTimeAdLoading = false
If you see the Logs, you'll know that onCreate
is called only once, and everytime the orientation changes after that, onConfigurationChanged()
is called, and thus, Activity is not recreated everytime you rotate the screen (I rotated my device thrice after initial loading).