Android application does not close after calling System.exit (0)

I have an Android app that worked great before I added admob activity. I close the application using the kill process (call System.exit (0)). I know this is the worst solution to complete the application. I work with OpenGL states and the libgdx framework, so I cannot fix all the memory leak that occurs when I call the standard android finish () function.

So the problem is:

My application usually works several times. I close and start it again and again. Everything works fine, but suddenly the admob view does not appear, and when I try to close, it freezes. The sound works, the last screen shows itself, but the touch does not work.

When I kill a process with the task manager, the music is still playing. Even when I completely uninstall the application, the music is still playing, so the activity is still working. It only stops when the phone restarts.

Without admob, everything works fine. I am also trying to destroy adView before closing with no result.

Here is my main activity:

public class ControllerActivity extends AndroidApplication{ private AdView adView; RelativeLayout layout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); AndroidApplicationConfiguration cfg = new AndroidApplicationConfiguration(); cfg.useGL20 = true; cfg.useCompass = false; cfg.useAccelerometer = false; layout = new RelativeLayout(this); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN); View gameView = initializeForView(Controller.getInstance(), cfg); adView = new AdView(this, AdSize.BANNER, "MYID"); AdRequest adRequest=new AdRequest(); adView.loadAd(adRequest); layout.addView(gameView); RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP); adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); layout.addView(adView, adParams); setContentView(layout); } @Override public void onDestroy() { if (adView!=null) { adView.stopLoading(); adView.destroy(); } System.exit(0); super.onDestroy(); } } 

Do you have any ideas how to completely kill this process?

+4
source share
4 answers

I found out some facts. If the application ends with System.exit(0) or android.os.Process.killProcess , then next time AdMob will not show ads. Even more, if you try to close the application, it will get stuck (the process remains active, and the only way to kill it is to reboot the device). The only solution is to not use System.exit(0) to exit the application. It should be noted that it does not matter if adView.destroy() or adView.stopLoading() called.

I used this.moveTaskToBack(true); instead of completing. It will hide the application, and if after a while it is not restored, Android will release all the resources, and AdMob will work fine. If the application is restored, it will continue from the same place.

+1
source

I continued testing, and it seems that this is a bug in the AdMob SDK 6.4.1. I downloaded 6.3.0 and this error went away.

+1
source

I usually use:

android.os.Process.killProcess (android.os.Process.myPid ())

to kill the process and all running actions.

This is for a class that extends the action. I'm not quite sure if it is different from the AndroidApplication extension

+1
source

Please note that this will work.

  <com.google.ads.AdView android:id="@+id/adView" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" ads:adSize="SMART_BANNER" ads:adUnitId="@string/admobid" ads:loadAdOnCreate="true" ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" android:background="@android:color/transparent" /> 

Or

You can call adView.stopLoading() when the activity is destroyed.

-one
source

Source: https://habr.com/ru/post/1501611/


All Articles