Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the Activity being destroyed because orientation changed or because app is closing?

I have an Activity that starts an AsyncTask. The activity is allowed to show in Portrait or Landscape orientation. When the orientation is changed, the Activity is destroyed and recreated. The task continues working no matter how many times the orientation is changed. It also returns the results to the activity successfully (according to CommonsWare's answer here http://goo.gl/WF1yW).

What I want to achieve is: when the activity is destroyed because the application is closing - the task should be cancelled. However, when the activity is destroyed because of an orientation change - the task should NOT be cancelled.

Basically the question is how to distinguish between the two cases: application is closing / orientation change. In both cases the onDestroy() method is called and there is no easy way to check something like isChangingOrientation()...

P.S. I can also consider a totally different approach if necessary.

like image 535
Stan Avatar asked Mar 05 '12 22:03

Stan


People also ask

What happens when screen orientation changes in Android?

When you rotate your device and the screen changes orientation, Android usually destroys your application's existing Activities and Fragments and recreates them. Android does this so that your application can reload resources based on the new configuration.

How do I stop Android from restarting activity when changing orientations?

If you want the activity to not restart during screen orientation change, you can use the below AndroidManifest. xml. Please note the activity android:configChanges=”orientation|screenSize” attribute. This attribute makes the activity not restart when change screen orientation.

Which method called when orientation changes android?

If you want to manually handle orientation changes in your app you must declare the "orientation" , "screenSize" , and "screenLayout" values in the android:configChanges attributes.


2 Answers

you can use isFinishing() method , to check if the activity is going to be killed or onDestroy() method just called due to change in orientation

@Override
protected void onDestroy() {
    super.onDestroy();
    if(isFinishing()){
        Log.i("DEBUG", "App will Terminate ");
    }else{
        Log.i("DEBUG", "Orientation changed");
    }

}
like image 77
Mina Fawzy Avatar answered Sep 30 '22 13:09

Mina Fawzy


I found a somewhat satisfying solution, which I want to share.

The method onRetainNonConfigurationInstance() is called only when the Activity is being recreated. More specifically: Called by the system, as part of destroying an activity due to a configuration change, when it is known that a new instance will immediately be created for the new configuration.

I override this method and store a flag (stating whether it has been called or not). Then in onDestroy (called shortly after) I check this flag and if it's false I cancel the background task, because the Activity is being destroyed for good. If it's true this means that onRetainNonConfigurationInstance() has been called, which means that the Activity is being recreated, so I leave the task running.

I would have liked a better solution, but could not find such. The problems with this solution are two: the method is deprecated; there is no guarantee that the method will be called (according to the documentation). In practice the solution works for me, so I'll use it...

like image 41
Stan Avatar answered Sep 30 '22 13:09

Stan