更改片段 / activity 时功能无法正常工作

Function not working as intended when changing fragment / activity

该应用程序用于带条形码的考勤。

扫描功能工作正常,直到我切换到其他片段/activity并返回到它。如果发生这种情况,那么相机将扫描但它不会 return 任何东西并且在返回时它不会显示任何吐司(甚至认为它应该)。 如果我关闭应用程序然后重新打开它,打开此片段并扫描,它会再次恢复正常,直到我更改为其他片段/activity.

没有任何错误信息。所以我不知道错误在哪里。

这是我的部分代码,我认为与此错误有关:

onCreateView:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_attendance, container, false);


    fetch();

    return v;
}

获取:

private void fetch() {

FirebaseRecyclerOptions <ModelEvent> options = new FirebaseRecyclerOptions.Builder<ModelEvent>()
.setQuery(query, snapshot -> new ModelEvent(
    Objects.requireNonNull(snapshot.child("eventID").getValue()).toString(),
    Objects.requireNonNull(snapshot.child("eventStatus").getValue()).toString()))
.build();

adapter = new FirebaseRecyclerAdapter<ModelEvent, ViewHolderEvent>(options) {
    @NonNull
    @Override
    public ViewHolderEvent onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.list_attendance, parent, false);

        return new ViewHolderEvent(view);
    }

    //Set data on each list
    @Override
    protected void onBindViewHolder(@NonNull ViewHolderEvent viewHolderEvent, int i, @NonNull ModelEvent modelEvent) {
            viewHolderEvent.setStringStatus(modelEvent.getEventStatus());

            viewHolderEvent.buttonScan.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    stringEventID = modelEvent.getEventID();

                    ScanOptions options = new ScanOptions();
                    options.setDesiredBarcodeFormats(ScanOptions.ALL_CODE_TYPES); 
                    options.setPrompt("Scan something");
                    options.setOrientationLocked(true);
                    options.setBeepEnabled(true);
                    fragmentLauncher.launch(options);
                }
            });
        }
    };
    recyclerViewAttendance.setAdapter(adapter);
}

扫描仪:

private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
    result -> {
        //On Backpress
        if(result.getContents() == null) {
            Toast.makeText(getContext(), "Cancelled", Toast.LENGTH_LONG).show();
        }
        else {
            //Get data from QRCode
            stringQR = result.getContents();
            
            getUserData();
            setUserAttendance();


            //Notification
            Toast.makeText(getContext(), "Name: " + stringName, Toast.LENGTH_LONG).show();
         }
    });

开始时:

@Override
public void onStart() {
    super.onStart();
    adapter.startListening();
}

这是完整代码:https://pastebin.com/e7XZN08i

所以我做了很多事情,最后找到了我应该做的。

 switch (item.getItemId()) {

    case R.id.nav_event:
        fragmentEvent = new FragmentEvent(); //i just need to add this on all case for some reason, the tutorial i use dont do this
        getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragmentEvent).commit();
        return true;

我也在 github 上问这个:https://github.com/journeyapps/zxing-android-embedded/issues/695