Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to distinguish between orientation change and leaving application android

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.

like image 279
Dustfinger Avatar asked Mar 08 '12 16:03

Dustfinger


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.

Which method of an activity is fired whenever there is a change in display orientation?

For any activity, you will have to save whatever state you need to save in the onResume() method, which is fired every time the activity changes an orientation.

How do I manage screen orientation on 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. You can declare multiple configuration values in the attribute by separating them with a pipe | character.

What will happen to the activity life cycle if we change the device orientation?

First onPause(), onStop() and onDestroy() will be called simultaneously and after some time when the activity restarts the onCreate(), onStart() and onReume() methods will be called simultaneously. So when you change the orientation of a screen the Activity will be restarted.


4 Answers

Use the Activity's isFinishing() method.

@Override
  protected void onDestroy() {
    super.onDestroy();
    if (isFinishing()) {
      // do stuff
    } else { 
      //It's an orientation change.
    }
  }
like image 198
dmon Avatar answered Oct 19 '22 07:10

dmon


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

like image 39
Mina Fawzy Avatar answered Oct 19 '22 09:10

Mina Fawzy


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

like image 4
Maurycy Avatar answered Oct 19 '22 08:10

Maurycy


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.

like image 1
seanhodges Avatar answered Oct 19 '22 09:10

seanhodges