Android在onCreate中保存微调器状态,在Activity onResume上运行

时间:2018-02-06 17:55:37

标签: java android spinner adapter zxing

我目前面临2个问题,微调器状态未保存,当我在InformationActivity上时,微调器被重置。我已经在其他帖子上使用了这些信息,但它似乎没什么帮助。

目前我无法在我finish(); onBackPressed(); InformationActivity上使用ScannerView,因为它在我实施ZXing库时会回到onBackPressed。我如何保存Spinner状态,并在我HomeActivity时阻止程序崩溃public class HomeActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{ private FirebaseAuth firebaseAuth; private Button buttonLogout; private ZXingScannerView scannerView; private final int permission_code = 1; String [] selectedProfile; Spinner spinner; ArrayAdapter<CharSequence> adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); firebaseAuth = FirebaseAuth.getInstance(); if(firebaseAuth.getCurrentUser() == null){ finish(); startActivity(new Intent(this, MainActivity.class)); } FirebaseUser user = firebaseAuth.getCurrentUser(); if (savedInstanceState != null) { spinner.setSelection(savedInstanceState.getInt("yourSpinner", 0)); // do this for each of your text views } else { //android spinner to select profile spinner = (Spinner) findViewById(R.id.spinnerProfiles); adapter = ArrayAdapter.createFromResource(this, R.array.restrictions, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_list_item_1); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(this); } } //opens camera when button is pressed public void scanBarcode(View view) { //check if user given app camera permissions if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, permission_code); } //opens camera scannerView = new ZXingScannerView(this); scannerView.setResultHandler(new ZXingScannerResultHandler()); //stops camera and scannerview setContentView(scannerView); scannerView.startCamera(); } //selects an item from the spinner and passes it to InformationActivity @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { String item = parent.getItemAtPosition(position).toString(); Toast.makeText(getBaseContext(), item + " Selected", Toast.LENGTH_SHORT).show(); switch (position){ case 0: selectedProfile = getResources().getStringArray(R.array.Wheat); break; case 1: selectedProfile = getResources().getStringArray(R.array.Crustaceans); break; case 2: selectedProfile = getResources().getStringArray(R.array.Eggs); break; } } @Override public void onNothingSelected(AdapterView<?> parent) { } //stops camera and outputs barcode result to a Toast class ZXingScannerResultHandler implements ZXingScannerView.ResultHandler { @Override public void handleResult(Result result) { String resultBarcode = result.getText(); Intent intent = new Intent(getApplicationContext(), InformationActivity.class); intent.putExtra("BarcodeString", resultBarcode.toString()); intent.putExtra("ProfileArray", selectedProfile); startActivity(intent); scannerView.stopCamera(); } } @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == permission_code) { if (grantResults[0] == PackageManager.PERMISSION_GRANTED) { Toast.makeText(this, "Permission Granted", Toast.LENGTH_LONG).show(); } else { Toast.makeText(this, "Permission Denied", Toast.LENGTH_LONG).show(); } } //go back to home after permissions accepted Intent intent = new Intent(getApplicationContext(), HomeActivity.class); startActivity(intent); scannerView.stopCamera(); } //goes back to homepage when back button is pressed @Override public void onBackPressed() { setContentView(R.layout.activity_home); scannerView.stopCamera(); } //stops the camera on pause @Override public void onPause(){ super.onPause(); scannerView.stopCamera(); } @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); outState.putInt("yourSpinner", spinner.getSelectedItemPosition()); } }

感谢。

HomeActivity:

public class InformationActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_information);

        TextView barcodeView = (TextView) findViewById(R.id.tvBarcode);
        barcodeView.setText(getIntent().getExtras().getString("BarcodeString"));

        TextView profileView = (TextView) findViewById(R.id.tvProfile);
        String[] selectedProfile = getIntent().getStringArrayExtra("ProfileArray");
        profileView.setText(selectedProfile[0]);


    }


    @Override
    public void onBackPressed() {
        Intent intent = new Intent(getApplicationContext(), HomeActivity.class);
        startActivity(intent);
    }
}

InformationActivity:

dateadd

2 个答案:

答案 0 :(得分:0)

在共享偏好中保存Spinner的位置:

int userChoice = spinner.getSelectedItemPosition();
SharedPreferences sharedPref = getSharedPreferences("FileName",0);
SharedPreferences.Editor prefEditor = sharedPref.edit();
prefEditor.putInt("userChoiceSpinner",usersChoice);
prefEditor.commit();

从共享偏好中获取数据:

SharedPreferences sharedPref = getSharedPreferences("FileName",MODE_PRIVATE);
int spinnerValue = sharedPref.getInt("userChoiceSpinner",-1);
if(spinnerValue != -1) {
  // set the selected value of the spinner 
  spinner.setSelection(spinnerValue);
}

另外看看:

另一个要保存的示例:https://stackoverflow.com/a/29527936/8448886

SharedPreference教程:https://www.journaldev.com/9412/android-shared-preferences-example-tutorial

答案 1 :(得分:0)

当您从微调器中选择任何项目时,获取所选项目并存储该位置,并在您返回活动后首先加载微调器,将该项目与数组项目进行比较并获取该项目的位置,然后将该位置设置为微调器我认为这样你可以精确选择那个项目进入微调器。