MainActivity未根据“设置”进行更新

时间:2013-03-06 09:58:26

标签: android android-activity

这是我的MainActivity ..

中的一些代码
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    updateConnectedFlags();
    SharedPreferences settings = PreferenceManager.getDefaultSharedPreferences(this);
    Editor editor = settings.edit();
    if(getSharedData("listPref").equals("")){
        editor.putString("listPref", "mode2"); 
    }
    if(getSharedData("listNetwork").equals("")){
        editor.putString("listNetwork", "Any"); 
    }
    if(getSharedData("updatePref").equals("")){
        editor.putString("updatePref", "300"); 
    }
   editor.commit();

   if(getSharedData("regStatus").equals(""))
   {
    setContentView(R.layout.activity_registration);

   }
   else if(getSharedData("loginStatus").equals(""))
   {
    if(getSharedData("rememberMe").equals("")){

        setContentView(R.layout.activity_login);
    }
    else{//if not remembered
        setContentView(R.layout.activity_login);
        EditText txtUserGet = (EditText) findViewById(R.id.txtUser);
        txtUserGet.setText(getSharedData("user"));

        EditText txtPasswordGet = (EditText) findViewById(R.id.txtPassword);
        txtPasswordGet.setText(getSharedData("password"));
    }   
   }
   else//If login status true
   {
    sPref=getSharedData("listNetwork");
        if (((sPref.equals(ANY)) && (wifiConnected || mobileConnected))|| ((sPref.equals(WIFI)) && (wifiConnected))) 
        {
                mode=getSharedData("listPref");
                LocationManager locationManager =(LocationManager) getSystemService(Context.LOCATION_SERVICE);
                final boolean gpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
                int time =Integer.parseInt(getSharedData("updatePref"));
                time=time/60;
                if (!gpsEnabled) {
                    buildToast("Gps not enabled");
                }
                else//If GPS enabled
                {
                    if(mode.equals("mode2"))
                    {
                        //buildText("Nothing here",15);
                        setContentView(R.layout.activity_main_mode2);
                        if(time==0){
                            buildToast("Application will track the Position of your phone and Update it to Server per "+getSharedData("updatePref")+" Seconds");
                        }
                        else{//Just checking HRS / SECs
                        buildToast("Application will track the Position of your phone and Update it to Server per "+time+" Minutes");
                        }

                            gpsLocation =requestUpdatesFromProvider(LocationManager.GPS_PROVIDER, 0);
                            mapView = (MapView) findViewById(R.id.mapView);

                              // enable Street view by default
                              mapView.setStreetView(true);
                              // enable to show Satellite view
                              mapView.setSatellite(true);
                              // enable to show Traffic on map
                              // mapView.setTraffic(true);
                              mapView.setBuiltInZoomControls(true);
                              mapController = mapView.getController();
                              mapController.setZoom(16); 

                    }

                }//if GPS enabled

        }
        else
        {

            setContentView(R.layout.error_layout);
            TextView tv = (TextView) findViewById(R.id.errorMsgTv);
            tv.setTextSize(15);
            if((!wifiConnected) && (mobileConnected)){
            tv.setText("Wifi not Connected");
            }
            else if((!wifiConnected) || (!mobileConnected)){
            tv.setText("Neither Wifi Nor GPRS/3G connected");
            }
            buildToast("Unable to Connect.Please check settings");
        }


    }


}
@Override
protected void onStart() {
    super.onStart();
}

@Override
protected void onResume() {
    super.onResume();
}

我的应用程序也有一个SettingsActivity。 我什么时候选择SettingsActivity,&对preference.xmlMainActivity进行任何更改 根据改变的偏好不会更新,但是当我改变方向时,一切都是最新的......什么是可能的原因???

1 个答案:

答案 0 :(得分:0)

您的设置似乎仅在创建MainActivity时才会被读取。如果您旋转屏幕,则会重新创建活动并再次读取您的设置。

您可以移动应用程序中从onCreate的{​​{1}}到onResume读取设置的部分。如果您从MainActivity切换回SettingsActivity

,则会更新设置