Prevent activity from onDestroy after entered onStop while executing heavy tasks with Lollipop and higher

285 views Asked by At

I have noticed interesting activity behaviour on different API:

  • API 21 and higher - when activity entered onPause state while some view is being displayed on top of it (some part of activity still can be visible for user or not), it entering onStop and onDestroy almost immediately. Those reaction cause force close of displayed on top view.
  • API 16 - activity do not entering onDestroy in the same situation. That's why on top view normally displayed.

So question is what was changed in new android APIs? What changes have been made in activity lifecycle?

The documentation says:

In situations where the system needs more memory it may kill paused processes to reclaim resources.

This could be an explanation but why does the system acts differently while executing my code in different APIs?

Is it possible to tell the system that I still need my activity?

I must say that my application executes heavy duties. This is the part of my code:

ACTIVITY CLASS

public class OverlayActivity extends Activity {

private static boolean isRunning = false;
private AdManager mManager;
public static void runActivity(Context context){
    if (isRunning)
        return;
    Intent intent = new Intent(context, OverlayActivity.class);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
    context.startActivity(intent);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
    getWindow().setLayout(-1, -1);
    super.onCreate(savedInstanceState);
    isRunning = true;
    Log.d("TAG", "OverlayActivity onCreate");
    mManager = new AdManager(this,"http://my.mobfox.com/request.php",
            "267d72ac3f77a3f447b32cf7ebf20673", true);
    mManager.setListener(new AdListener() {
        @Override
        public void adClicked() {
            Log.d("TAG", "AdListener adClicked");
        }

        @Override
        public void adClosed(Ad ad, boolean b) {
            Log.d("TAG", "AdListener adClosed");
            OverlayActivity.this.finish();
        }

        @Override
        public void adLoadSucceeded(Ad ad) {
        if (mManager != null && mManager.isAdLoaded())
            mManager.showAd();
        }

        @Override
        public void adShown(Ad ad, boolean b) {
            Log.d("TAG", "AdListener adShown");
        }

        @Override
        public void noAdFound() {
            Log.d("TAG", "AdListener noAdFound");
            OverlayActivity.this.finish();
        }
    });
    mManager.requestAd();
}

@Override
protected void onStart() {
    super.onStart();
    Log.d("TAG", "OverlayActivity onStart");
}

@Override
protected void onResume() {
    super.onResume();
    Log.d("TAG", "OverlayActivity onResume");
}

@Override
protected void onPause() {
    super.onPause();
    Log.d("TAG", "OverlayActivity onPause");
}

@Override
protected void onStop() {
    super.onStop();
    Log.d("TAG", "OverlayActivity onStop");
}

@Override
protected void onDestroy() {
    Log.d("TAG", "OverlayActivity onDestroy");
    isRunning = false;
    super.onDestroy();
    if (mManager!=null)
        mManager.release();
    mManager=null;
    }
}

MANIFEST DECLARATION

        <activity android:name=".OverlayActivity"
        android:excludeFromRecents="true"
        android:noHistory="true"
        android:theme="@android:style/Theme.Translucent.NoTitleBar"
        android:finishOnTaskLaunch="true"/>

ACTIVITY LAUNCH POINT

public class AdRequestReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {        
    OverlayActivity.runActivity(context);
    }
}

Please, give me some information about changes in new android APIs that can cause such different activity behaviour.

Any help will be appreciated.

0

There are 0 answers