Skip to content

Commit

Permalink
Cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
rkistner committed Oct 25, 2021
1 parent 0c97cf7 commit a13ce2b
Showing 1 changed file with 2 additions and 21 deletions.
23 changes: 2 additions & 21 deletions sample/src/main/java/example/zxing/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,30 +128,18 @@ public void about(View view) {
* Sample of scanning from a Fragment
*/
public static class ScanFragment extends Fragment {
private String toast;

private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
toast = "Cancelled from fragment";
Toast.makeText(getContext(), "Cancelled from fragment", Toast.LENGTH_LONG).show();
} else {
toast = "Scanned from fragment: " + result.getContents();
Toast.makeText(getContext(), "Scanned from fragment: " + result.getContents(), Toast.LENGTH_LONG).show();
}

// At this point we may or may not have a reference to the activity
displayToast();
});

public ScanFragment() {
}

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

displayToast();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Expand All @@ -164,12 +152,5 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container,
public void scanFromFragment() {
fragmentLauncher.launch(new ScanOptions());
}

private void displayToast() {
if(getActivity() != null && toast != null) {
Toast.makeText(getActivity(), toast, Toast.LENGTH_LONG).show();
toast = null;
}
}
}
}

0 comments on commit a13ce2b

Please sign in to comment.