这有可能检查被称为的onCreate由于取向的变化? [英] Is that possible to check was onCreate called because of orientation change?

查看:185
本文介绍了这有可能检查被称为的onCreate由于取向的变化?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要采取不同的 ONSTART()方法取决于如何的onCreate()被称为结果 对方向变化与否。这是可能的检查吗?

其实我需要的是做的东西时,活动的开始,除非情况下,当用户改变方向

我要明确区分情况下,当用户只需切换到另一个应用程序,并返回背部或他改变设备的方向。

解决方案

 公共类MyActivity延伸活动{
    布尔fromOrientation = FALSE;
    共享preferences我的prefLogin;
    编辑prefsEditor;
    @覆盖
    公共无效的onCreate(包savedInstanceState){
        super.onCreate(savedInstanceState);
        prefsEditor =我的prefLogin.edit();
        我的prefLogin = this.getShared preferences(我的preFS,Context.MODE_WORLD_READABLE);
        fromOrientation =我的prefLogin.getString(fromOrient,假);

        如果(fromOrientation){
             //做根据需要---定向逻辑改变。
        } 其他 {
             //做根据需要---默认的onCreate的逻辑()。
        }
    }

    @覆盖
    公共对象onRetainNonConfigurationInstance(){
        prefsEditor.putString(fromOrient,真正的);
        prefsEditor.commit();
        返回null;
    }

    @覆盖
    保护无效的onDestroy(){
        如果(fromOrientation){
            prefsEditor.putString(fromOrient,假);
            prefsEditor.commit();
        }
        super.onDestroy();
    }
}
 

标记 fromOrientation (讲述了状态,以配置已经改不) 上面我们喜欢的defalut我们正在考虑的onCreate做了()不是从改变方向调用。如果方向改变( onRetainNonConfigurationInstance()),那么我们设置它让我们知道,在preference标志值是否的onCreate()从上方向或不叫。 最后的onDestroy()我们再次重置方向标志。

I need to act differently in onStart() method depending on how onCreate() was called in result of orientation change or not. Is that possible to check it?

Actually what I need is to do something when activity starts except a case when user changes orientation.

I need to clearly distinguish cases when user just switched to another application and returned back OR he changed device's orientation.

解决方案

public class MyActivity extends Activity {
    boolean fromOrientation=false;
    SharedPreferences myPrefLogin;
    Editor prefsEditor;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        prefsEditor = myPrefLogin.edit();
        myPrefLogin = this.getSharedPreferences("myPrefs", Context.MODE_WORLD_READABLE);
        fromOrientation = myPrefLogin.getString("fromOrient", false);

        if(fromOrientation) {
             // do as per need--- logic of orientation changed.
        } else {
             // do as per need--- logic of default onCreate().
        }
    }

    @Override
    public Object onRetainNonConfigurationInstance() {
        prefsEditor.putString("fromOrient", true);
        prefsEditor.commit();
        return null;
    }

    @Override
    protected void onDestroy() {
        if(fromOrientation) {
            prefsEditor.putString("fromOrient", false);
            prefsEditor.commit();
        }
        super.onDestroy();
    }  
}

flag fromOrientation(tells the state either configuration had been change or not) above we had done like by defalut we are considering that onCreate() is not called from by changing orientation. If orientation changed(onRetainNonConfigurationInstance()) then we set the flag value in preference which let us know whether onCreate() is called from on orientation or not. And finally onDestroy() we again reset the orientation flag.

这篇关于这有可能检查被称为的onCreate由于取向的变化?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆