保存实际进度progressBar onSaveInstanceState

时间:2017-05-15 13:15:36

标签: java android

我在onSaveInstanceState上保存当前进度条时遇到问题。我节省当前时间,但退出活动并返回 恢复预先不要显示离开的时间。

此课程显示进度条

public class CardDetailsActivity extends AppCompatActivity {
    private static final String TIME = "Time ";

    @Override
    protected void attachBaseContext(Context newBase) {
        super.attachBaseContext(newBase);
    }

    private ProgressBar progressBar;
    private TextView secondTimeTextView;
    private int interval, currentInterval;
    protected String cardName, intervalTotp, path3Decoded, getCode, uuidDevice, otpString, generateNewString, generateQrString, OtpString, NewXorString;


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



  if (savedInstanceState != null){
            currentInterval = savedInstanceState.getInt(TIME);
            progressBar.setProgress(currentInterval);
        }

        Typeface custom_fonts = Typeface.createFromAsset(getAssets(), "fonts/OpenSans-Regular.ttf");
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setNavigationIcon(R.drawable.ic_restart);
        progressBar = (ProgressBar) findViewById(R.id.refershProgrss);

        cardName = getIntent().getStringExtra("cardName");
        intervalTotp = getIntent().getStringExtra("intervalTotp");
        path3Decoded = getIntent().getStringExtra("path");
        otpString = getIntent().getStringExtra("otp");

        Base32 code = new Base32();
        byte secret[] = code.decode(otpString);

        try {
            getCode = OtpAlgorithm.generateTotp(secret, intervalTotp);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (InvalidKeyException e) {
            e.printStackTrace();
        }

        OtpString = path3Decoded + getCode;
        generateQrString = Cryptography.xorHex(OtpString);
        interval = Integer.parseInt(intervalTotp);

        TextView titleTextView = (TextView) findViewById(R.id.toolbar_title);
        titleTextView.setText(cardName);

        TextView subtitleTextView = (TextView) findViewById(R.id.subtitle_text);
        TextView second_text = (TextView) findViewById(R.id.second_text);
        TextView instantTextView = (TextView) findViewById(R.id.instant_text);
        instantTextView.setTypeface(custom_fonts);
        titleTextView.setTypeface(custom_fonts);

        TextView generationCardTextView = (TextView) findViewById(R.id.generationCardText);
        generationCardTextView.setTypeface(custom_fonts);


        secondTimeTextView = (TextView) findViewById(R.id.secondTimeTextView);
        secondTimeTextView.setTypeface(custom_fonts);

        TextView dotTextView = (TextView) findViewById(R.id.dotTextView);
        dotTextView.setTypeface(custom_fonts);
        subtitleTextView.setTypeface(custom_fonts);
        second_text.setTypeface(custom_fonts);
        setSupportActionBar(toolbar);

        UpdateQrCodeString(generateQrString);
        new UpdateQrCode().execute();

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.card:
                Intent intent = new Intent(this, MainActivity.class);
                startActivity(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.card, menu);
        return super.onCreateOptionsMenu(menu);
    }


    private class UpdateQrCode extends AsyncTask<Void, Integer, Integer> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            progressBar.setMax(interval);
        }

        @Override
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
            progressBar.setProgress(values[0]);
            currentInterval = values[0];
            secondTimeTextView.setText(String.valueOf(values[0]));

        }

        @Override
        protected Integer doInBackground(Void... params) {

            for (int i = interval; i >= 0; i--) {
                publishProgress(i);

                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }

            return null;
        }

        @Override
        protected void onPostExecute(Integer integer) {
            super.onPostExecute(integer);


            try {
                NewXorString = generateQrCodeString(otpString, path3Decoded, intervalTotp);

            } catch (InvalidKeyException e) {
                e.printStackTrace();
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }

            generateNewString = Cryptography.xorHex(NewXorString);


            UpdateQrCodeString(generateNewString);
            new UpdateQrCode().execute();

        }
    }

    // function with update QR Code Image

    private void UpdateQrCodeString(String generateStringQrCode) {

        WindowManager manager = (WindowManager) getSystemService(WINDOW_SERVICE);
        Display display = manager.getDefaultDisplay();
        Point point = new Point();
        display.getSize(point);

        int width = point.x;
        int height = point.y;

        int smallerDimension = width < height ? width : height;
        smallerDimension = smallerDimension * 3 / 4;

        QRCodeEncoder qrCodeEncoder = new QRCodeEncoder(generateStringQrCode, null, Contents.Type.TEXT, BarcodeFormat.QR_CODE.toString(), smallerDimension);

        try {
            Bitmap bitmap = qrCodeEncoder.encodeAsBitmap();
            ImageView myImageView = (ImageView) findViewById(R.id.qrCodeImageView);
            myImageView.setImageBitmap(bitmap);
        } catch (WriterException e) {
            e.printStackTrace();
        }

    }

    private String generateQrCodeString(String otpString, String path3String, String intervalTotp) throws InvalidKeyException, NoSuchAlgorithmException {

        Base32 code = new Base32();
        byte secret[] = code.decode(otpString);
        String totpString = OtpAlgorithm.generateTotp(secret, intervalTotp);
        return path3String + totpString;

    }

  @Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    outState.putInt(TIME, currentInterval);
    progressBar.setProgress(currentInterval);
}


}

1 个答案:

答案 0 :(得分:1)

Docs。如您所见,在onStart之后调用了onRestoreInstanceState。正如文档指出的最简单方法是使用onCreate

if(savedInstance != null){
currentInterval = savedInstanceState.getInt(TIME);
progressBar.setProgress(currentInterval);
}

编辑:我做了一个编辑