片段上的Android横向方向重置共享首选项

时间:2015-07-26 14:34:34

标签: android android-layout android-fragments sharedpreferences android-sensors

我正在使用共享首选项来跟踪保存在设备上的管理员代码,我注意到当我去这个具有横向方向的特定片段时,首选项被调用两次,第二次重置为它的初始空值。这是它发生的唯一片段,我已经能够将其缩小到行

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        final Activity activity = getActivity();

        SharedPreferences sharedpreferences = activity.getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
        Boolean Admin_Mode = sharedpreferences.getBoolean("AdminCode", false);
        String IPaddress = sharedpreferences.getString("IP Address", "");

        System.out.println(IPaddress);
        System.out.println(Admin_Mode);

        View rootView = inflater.inflate(R.layout.augmented_reality_view, container, false);

        ActionBar actionBar = ((ActionBarActivity)activity).getSupportActionBar();

        actionBar.hide();

        System.out.println("here");


        activity.setRequestedOrientation(
                ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        mSensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);

        mRotationVectorSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ROTATION_VECTOR);
        mMagneticSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);


        mSensorManager.registerListener(this, mRotationVectorSensor, 10000);
        mSensorManager.registerListener(this, mMagneticSensor, 10000);

        HeadTracker = (ToggleButton) rootView.findViewById(R.id.HeadTracker);


        return rootView;
    }

    @Override
    public void onSensorChanged(SensorEvent event) {

        if (HeadTracker.isChecked() == true) {

            if (event.sensor.getType() == Sensor.TYPE_ROTATION_VECTOR) {

                SensorManager.getRotationMatrixFromVector(
                        mRotationMatrix, event.values);

                if (mRotationMatrix[2] >= 0.6 && mRotationMatrix[0] >= -0.1 && mRotationMatrix[0] <= 0.2){

                    Left = true;
                    Right = false;

                }
                else if (mRotationMatrix[2] <= -0.8 && mRotationMatrix[0] >= -0.1 && mRotationMatrix[0] <= 0.2){

                    Left = false;
                    Right = true;
                }
                else{
                    Left = false;
                    Right = false;;
                }

            }

        }

        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD) {

            if(event.values[2] >= 390){
                MagnetButtonPressed = true;
            }
            else{
                MagnetButtonPressed = false;
            }

        }

    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }

    @Override
    public void onPause()
    {
        super.onPause();
        mSensorManager.unregisterListener(this);
    }

是否有一种特殊的正确方法可以使用横向方向使用共享首选项

这是代码

public class Settings extends PreferenceFragment {

public SharedPreferences sharedpreferences;
public static final String MyPREFERENCES = "MyPrefs" ;

String IPaddress;
int PortNumber;
Boolean Admin_Mode;


public Settings() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    View rootView = inflater.inflate(R.layout.settings, container, false);

    getActivity().setRequestedOrientation(
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);


    final SharedPreferences sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES,Context.MODE_PRIVATE);

    IPaddress = sharedpreferences.getString("IP Address","");
    PortNumber = sharedpreferences.getInt("Port Numner", 1);
    Admin_Mode = sharedpreferences.getBoolean("AdminCode", false);

    final EditText mEdit   = (EditText)rootView.findViewById(R.id.ipaddress);
    final EditText mEdit2   = (EditText)rootView.findViewById(R.id.portnumber);
    final EditText AdminCommandBox = (EditText)rootView.findViewById(R.id.AdminCommandBox);

    mEdit.setText(IPaddress);
    String strI = Integer.toString(PortNumber);
    mEdit2.setText(strI);


    Button clickButton = (Button) rootView.findViewById(R.id.Update_Settings);
    clickButton.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {


            SharedPreferences.Editor editor = sharedpreferences.edit();
            editor.putString("IP Address", mEdit.getText().toString());
            editor.putInt("Port Numner", Integer.parseInt(mEdit2.getText().toString()));
            editor.commit();

            Toast.makeText(getActivity(),"Port and Ip Updated!",Toast.LENGTH_SHORT).show();


        }
    });

    final Button Authorise = (Button) rootView.findViewById(R.id.Authorise_Button);

    if (Admin_Mode == false){Authorise.setText("Authorise");}
    else if (Admin_Mode == true){Authorise.setText("Deactivate");}

    Authorise.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if (Admin_Mode == false){

                if (AdminCommandBox.getText().toString().equals("FerasQUT123")) {

                    SharedPreferences.Editor editor = sharedpreferences.edit();
                    editor.putBoolean("AdminCode", true);
                    editor.commit();

                    Toast.makeText(getActivity(),"Code Authorised",Toast.LENGTH_SHORT).show();

                    Authorise.setText("Deactivate");

                }

                else{

                    Toast.makeText(getActivity(),"Please Enter the Correct Code",Toast.LENGTH_SHORT).show();

                }


            }
            else if (Admin_Mode == true){

                SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putBoolean("AdminCode", false);
                editor.commit();

                Toast.makeText(getActivity()," Deactivated",Toast.LENGTH_SHORT).show();

                Authorise.setText("Authorise");

            }


        }
    });


    return rootView;
}



public void onDestroy() {
    super.onDestroy();

}}

任何建议都是史诗:)

干杯

史蒂夫

/// ......编辑......... \\\ 添加在设置片段代码

foreach ($response['results']['collection'] as $textlink) {
 $row = $textlink['textlink'];
 echo $row['href'];
 echo "<br>";
 echo $row['text'];
 echo "<br>";
}

1 个答案:

答案 0 :(得分:0)

  

在共享首选项中,我在on创建开始时获得管理模式的状态,我将其在主活动中设置为false,然后在输入正确的代码时在设置片段中更新它。

您的问题是您在活动的onCreate方法中将管理模式设置为 false 。当屏幕方向发生变化时,活动将被销毁并重新创建 - 因此将再次调用onCreate,这会将您的管理模式首选项设置为false。