Skip to content

Commit

Permalink
Fixed crash when rotating the ResultActivity.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kamuno committed Feb 27, 2020
1 parent 8890b3e commit 15aae4d
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.secuso.privacyfriendlycodescanner.qrscanner.ui.activities;

import android.app.Activity;
import android.arch.lifecycle.ViewModelProviders;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
Expand Down Expand Up @@ -40,6 +41,7 @@
import com.secuso.privacyfriendlycodescanner.qrscanner.ui.resultfragments.TextResultFragment;
import com.secuso.privacyfriendlycodescanner.qrscanner.ui.resultfragments.URLResultFragment;
import com.secuso.privacyfriendlycodescanner.qrscanner.ui.resultfragments.WifiResultFragment;
import com.secuso.privacyfriendlycodescanner.qrscanner.ui.viewmodel.ResultViewModel;

import static com.secuso.privacyfriendlycodescanner.qrscanner.helpers.PrefManager.PREF_SAVE_REAL_IMAGE_TO_HISTORY;

Expand Down Expand Up @@ -76,6 +78,7 @@ public static void startResultActivity(@NonNull Context context, @NonNull Histor
}

private SharedPreferences mPreferences;
private ResultViewModel viewModel;

private BarcodeResult currentBarcodeResult = null;
private ResultFragment currentResultFragment = null;
Expand All @@ -92,6 +95,8 @@ protected void onCreate(Bundle savedInstanceState) {

mPreferences = PreferenceManager.getDefaultSharedPreferences(this);

viewModel = ViewModelProviders.of(this).get(ResultViewModel.class);

initOrRestoreState(savedInstanceState);

ActionBar ab = getSupportActionBar();
Expand Down Expand Up @@ -148,6 +153,12 @@ private void initOrRestoreState(Bundle savedInstanceState) {
mSavedToHistory = savedInstanceState.getBoolean("mSavedToHistory");
mParsedResult = ResultParser.parseResult(currentHistoryItem.getResult());
}
viewModel.currentBarcodeResult = currentBarcodeResult;
viewModel.currentHistoryItem = currentHistoryItem;
viewModel.currentResultFragment = currentResultFragment;
viewModel.mParsedResult = mParsedResult;
viewModel.mCodeImage = mCodeImage;
viewModel.mSavedToHistory = mSavedToHistory;
}


Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,41 @@
package com.secuso.privacyfriendlycodescanner.qrscanner.ui.resultfragments;

import android.arch.lifecycle.ViewModelProviders;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;

import com.google.zxing.client.result.ParsedResult;
import com.secuso.privacyfriendlycodescanner.qrscanner.ui.viewmodel.ResultViewModel;

public abstract class ResultFragment extends Fragment {

protected ParsedResult parsedResult;
protected ResultViewModel viewModel;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

viewModel = ViewModelProviders.of(getActivity()).get(ResultViewModel.class);
}

@Override
public void onAttach(Context context) {
super.onAttach(context);

viewModel = ViewModelProviders.of(getActivity()).get(ResultViewModel.class);
parsedResult = viewModel.mParsedResult;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);

viewModel = ViewModelProviders.of(getActivity()).get(ResultViewModel.class);
parsedResult = viewModel.mParsedResult;
}

public void putQRCode(@NonNull ParsedResult parsedResult) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.secuso.privacyfriendlycodescanner.qrscanner.ui.viewmodel;

import android.arch.lifecycle.ViewModel;
import android.graphics.Bitmap;

import com.google.zxing.client.result.ParsedResult;
import com.journeyapps.barcodescanner.BarcodeResult;
import com.secuso.privacyfriendlycodescanner.qrscanner.database.HistoryItem;
import com.secuso.privacyfriendlycodescanner.qrscanner.ui.resultfragments.ResultFragment;

public class ResultViewModel extends ViewModel {

public BarcodeResult currentBarcodeResult = null;
public ResultFragment currentResultFragment = null;

public HistoryItem currentHistoryItem = null;
public ParsedResult mParsedResult = null;
public Bitmap mCodeImage = null;
public boolean mSavedToHistory = false;

}

0 comments on commit 15aae4d

Please sign in to comment.