How to distinguish between orientation change and leaving application android
Asked Answered
R

4

54

I understand that when the orientation of the screen changes, the current activities onDestroy() is called followed by the onCreate() to effectively recreate the activity. I need to know how to programmatically tell if the application is being exited or if just the orientation is being changed.

One method is for the previous activity to notify me when its onResume() method is being called, this will let me know that the user has pressed the back button and the orientation hasn't been changed.

P.S. I'm looking for a solution more elegant than listening for a back hardware button click.

Here was what i wanted to do:

I have two tabs, when the activity is being entered for the first time or the user has left the activity and is now entering it, a certain tab is displayed based on some criterion.

When the orientation changes, i need to stay on the same tab.

Rosario answered 8/3, 2012 at 16:9 Comment(6)
Can you just indicate in the manifest that you want to handle orientation changes yourself? Then the onDestroy / onCreate will not be called - and instead your activity's onConfigurationChange will be called. You can do whatever is necessary in there.Papilla
What Aleks suggested is to add to your manifest android:configChanges, for orientation. Then you can overwrite onConfigurationChange and handle the orientation changeJesher
@AleksG: Quoting the documentation for android:configChanges, "Using this attribute should be avoided and used only as a last-resort."Mandatory
"I need to know how to programmatically tell if the application is being exited or if just the orientation is being changed." -- why?Mandatory
I thought of handling orientation change myself at first but it seemed like a lot of work when logic dictates that 'leaving the application' and 'orientation change' should be differentiable by the activity life-cycle. Guess I'll have to do it that way then(As a last resort).Rosario
"When the orientation changes, i need to stay on the same tab." -- put your tab ID in the Bundle supplied to onSaveInstanceState(), and restore the selected tab in onRestoreInstanceState().Mandatory
C
101

Use the Activity's isFinishing() method.

@Override
  protected void onDestroy() {
    super.onDestroy();
    if (isFinishing()) {
      // do stuff
    } else { 
      //It's an orientation change.
    }
  }
Circlet answered 8/3, 2012 at 16:23 Comment(6)
Thanks, I read up about isFinishing() and its what i needed. See my editRosario
To save the current tab you can save its ID in the onSaveInstanceState(), then read it back in onCreate(), that way you don't even need to bother with the isFinishing(). Be careful though, the bundle will be null when starting the application for the first time.Circlet
One side-effect of this approach: If you switch to another app it will not finish your active one (so this will not be called). If the app is then closed by the Android OS to save memory - this method WILL be called, isFinishing() will be FALSE (see here: developer.android.com/reference/android/app/…) thus incorrectly executing your orientation change code.Diplomat
I had already tried using the onSaveInstanceState() and reading it back in onCreate() like you said but the problem still was I didn't know if onCreate() was being called by an orientation change or by the user navigating to the activity for the first time.Rosario
If it's the first time, the bundle should be null. Even if it wasn't null (for some reason), your saved tab value shouldn't be there, so you would load the "default" tab.Circlet
I have the same question but about Fragments.Adoree
L
23

you can use isChangingConfigurations() Read from documentation

Check to see whether this activity is in the process of being destroyed in order to be recreated with a new configuration. This is often used in onStop() to determine whether the state needs to be cleaned up or will be passed on to the next instance of the activity via onRetainNonConfigurationInstance().

Returns If the activity is being torn down in order to be recreated with a new configuration, returns true; else returns false

Explain in simple way with example

isChangingConfigurations()

is method used to check if the activity will be destroyed to be re-created again (as result of change in orientation )

How to use it ?

if you use api >= 11 then no problem , but if you use api < 11 then we must handle this method manual I make boolean variable called IsconfigChange

private boolean IsconfigChange ;
...

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        IsconfigChange = true ;

}


    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override
    public boolean isChangingConfigurations() {
        if(android.os.Build.VERSION.SDK_INT >= 11){
            Log.i("DEBUG", "Orientation changed api >= 11 ");
            return super.isChangingConfigurations();    
        }else {
            Log.i("DEBUG", "Orientation changed api < 11 ");
            return IsconfigChange; 
        }
    }

    @TargetApi(Build.VERSION_CODES.HONEYCOMB) 
    @Override
    protected void onStop() {
        super.onStop();
        if(isChangingConfigurations()){
            Log.i("DEBUG", "isChangingConfirgurations OnStop Called");
        }  else{
            Log.i("DEBUG", "OnStop Called");
        }
    }

Summery

you can use isChangingConfigurations in onStop to check if app stop to be destroyed or due to change in orientation .

or you can use isFinishing check my answer here

Landeros answered 4/3, 2015 at 12:18 Comment(0)
M
5

for API lvl >= 11 Activity has a isChangingConfigurations() method

Milore answered 3/11, 2014 at 21:48 Comment(0)
D
1

You could grab the value of the Activity.getChangingConfigurations() method in your onDestroy callback. This will return a result such as ORIENTATION_PORTRAIT, which you could check against your current orientation.

Note that the activity closing and orientation changes aren't the only conditions to consider here: What about returning to the Home screen, incoming phone calls and other apps stealing the focus, and all the other scenarios when your Activity is no longer at the front of the stack?

Most of the time you will not need to do this. If you are trying to fix some Activity state issue (often manifesting as a NullPointerException when you rotate the screen) by capturing the orientation event; review the Android Activity lifecycle and make sure you are not just trying to hack a fix for a design flaw. Post up your original problem on this site if you are unsure.

Diplomat answered 8/3, 2012 at 16:23 Comment(4)
Thanks. If you have a better way of implementing what i want to do than using isFinishing() let us know.Rosario
I think this is the better way that isFinishing() for the reasons I explained in the dmon's answer, but why do you want to do it at all? It's rare that an Android app actually needs to know when it is changing orientation, assuming you are adhering to the development guidelines...Diplomat
see the edited question, i tried to explain what i need to do.Rosario
Ah, sorry, I missed the edit. If I were you I'd follow @CommonsWare's suggestion in the comments. See this solution: https://mcmap.net/q/339773/-tab-change-on-screen-rotation-androidDiplomat

© 2022 - 2024 — McMap. All rights reserved.