How to distinguish between orientation change and leaving application android

。_饼干妹妹 提交于 2019-11-27 19:15:16

Use the Activity's isFinishing() method.

@Override
  protected void onDestroy() {
    super.onDestroy();
    if (isFinishing()) {
      // do stuff
    } else { 
      //It's an orientation change.
    }
  }
Mina Fawzy

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

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

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!