I am trying to implement ads in my android application. I suspected memory leaks when i added ads to my xml layout so i tried the approach described in this post :- Admob Memory Leak - avoiding by using empty activity
The ads are rendered and there is no problem except that if in between my app's screens, if i click on home button of the device accidently and then if i go to recent applications and select mine, I get the error :-
03-22 22:17:56.604: E/AndroidRuntime(27206): Caused by: java.lang.IllegalStateException: This activity should be created only once during the entire application life
03-22 22:17:56.604: E/AndroidRuntime(27206): at com.xyz.watch.AdMobActivity.<init>(AdMobActivity.java:16)
03-22 22:17:56.604: E/AndroidRuntime(27206): at java.lang.Class.newInstanceImpl(Native Method)
03-22 22:17:56.604: E/AndroidRuntime(27206): at java.lang.Class.newInstance(Class.java:1409)
03-22 22:17:56.604: E/AndroidRuntime(27206): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-22 22:17:56.604: E/AndroidRuntime(27206): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
03-22 22:17:56.604: E/AndroidRuntime(27206): ... 11 more
03-22 22:17:56.684: W/System.err(27206): java.io.FileNotFoundException: /data/plog.log (Permission denied)
The launch mode of my AdMobActivity is singleInstance so maybe thats the reason. What should i do for this to work?
Update :- My code
Inside my first launch activity (MainActivity)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseAdapter databaseAdapter = new DatabaseAdapter(
getApplicationContext());
databaseAdapter.open();
databaseAdapter.close();
}
@Override
protected void onResume() {
super.onResume();
if (AdMobActivity.AdMobMemoryLeakWorkAroundActivity == null) {
Log.i("CHAT", "starting the AdMobActivity");
AdMobActivity.startAdMobActivity(this);
}
}
AdMobActivity:-
public final class AdMobActivity extends Activity {
public static AdMobActivity AdMobMemoryLeakWorkAroundActivity;
public AdMobActivity() {
super();
if (AdMobMemoryLeakWorkAroundActivity != null) {
throw new IllegalStateException("This activity should be created only once during the entire application life");
}
AdMobMemoryLeakWorkAroundActivity = this;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("CHAT", "in onCreate - AdMobActivity");
finish();
}
public static final void startAdMobActivity(Activity activity) {
Log.i("CHAT", "in startAdMobActivity");
Intent i = new Intent();
i.setComponent(new ComponentName(activity.getApplicationContext(), AdMobActivity.class));
activity.startActivity(i);
}
}
Line no. 16 is
throw new IllegalStateException("This activity should be created only once during the entire application life");