Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ActivityResultLauncher not run when i switch from other fragment #695

Open
knight109 opened this issue Feb 3, 2022 · 4 comments
Open

ActivityResultLauncher not run when i switch from other fragment #695

knight109 opened this issue Feb 3, 2022 · 4 comments

Comments

@knight109
Copy link

Description of the problem:

This app is for Attendance using barcode. I use firebase on this app.
The app work as intended, i can scan the barcode and save data to firebase.
But the problem is that when i open another Fragment or Activity then back to this Fragment, the scanner dont work as intended. It does open the camera but anything inside ActivityResultLauncher not working.
Closing and reopening the app will fix this until i change to other Fragment or Activity again.

Which library version are you using?
4.3.0

Does the same happen on other devices or an emulator?
Yes, bot emulator and my device.

Can you reproduce the issue in the sample project included with the library? If not, can you
provide your own sample project or sample code that produces this error?

This is the code on my fragment: https://pastebin.com/e7XZN08i

In the case of an error do you have a stack trace or adb logs?
I made some Logs to check on where the progress at.
This the log when working as intended:
2022-02-03 16:01:07.464 20399-20399 I/ContentValues: On Create .....
2022-02-03 16:01:07.471 20399-20399 I/ContentValues: On Start .....
2022-02-03 16:01:12.673 20399-20399 I/ContentValues: On Scanner Initialization.....
2022-02-03 16:01:12.683 20399-20399 I/ContentValues: On Pause .....
2022-02-03 16:01:13.776 20399-20399 I/ContentValues: On Stop .....
2022-02-03 16:01:17.328 20399-20399 I/ContentValues: On Start .....
2022-02-03 16:01:17.328 20399-20399 I/ContentValues: On Scanner Launcher.....
2022-02-03 16:01:21.320 20399-20399 I/ContentValues: On Pause .....
2022-02-03 16:01:21.323 20399-20399 I/ContentValues: On Stop .....
2022-02-03 16:01:21.376 20399-20399 I/ContentValues: On Destroy .....

This when it not working as intended:
2022-02-03 16:01:26.439 20399-20399 I/ContentValues: On Create .....
2022-02-03 16:01:26.441 20399-20399 I/ContentValues: On Start .....
2022-02-03 16:01:27.924 20399-20399 I/ContentValues: On Scanner Initialization.....
2022-02-03 16:01:27.931 20399-20399 I/ContentValues: On Pause .....
2022-02-03 16:01:28.841 20399-20399 I/ContentValues: On Stop .....
2022-02-03 16:01:30.567 20399-20399 I/ContentValues: On Start .....
2022-02-03 16:01:32.785 20399-20399 I/ContentValues: On Pause .....
2022-02-03 16:01:32.786 20399-20399 I/ContentValues: On Stop .....
2022-02-03 16:01:32.827 20399-20399 I/ContentValues: On Destroy .....

@rkistner
Copy link
Member

rkistner commented Feb 3, 2022

The same thing is working on the sample application:

public static class ScanFragment extends Fragment {
private final ActivityResultLauncher<ScanOptions> fragmentLauncher = registerForActivityResult(new ScanContract(),
result -> {
if(result.getContents() == null) {
Toast.makeText(getContext(), "Cancelled from fragment", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(getContext(), "Scanned from fragment: " + result.getContents(), Toast.LENGTH_LONG).show();
}
});
public ScanFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_scan, container, false);
Button scan = view.findViewById(R.id.scan_from_fragment);
scan.setOnClickListener(v -> scanFromFragment());
return view;
}
public void scanFromFragment() {
fragmentLauncher.launch(new ScanOptions());
}
}

I'd recommend creating a minimal application that can still reproduce the issue - remove the database and firebase logic, as well as any views not directly relevant to this. Then compare it to the sample application and see where it's different to isolate the cause.

@knight109
Copy link
Author

knight109 commented Feb 4, 2022

I'd recommend creating a minimal application that can still reproduce the issue - remove the database and firebase logic, as well as any views not directly relevant to this. Then compare it to the sample application and see where it's different to isolate the cause.

It still happen with the minimal application.
ActivityResultLauncher still not running if i change to other fragment or activity.
I use the same code as above and put some Logs, this is my code: https://pastebin.com/ke9BJkmu
The Logs result is same as before.

@rkistner
Copy link
Member

rkistner commented Feb 7, 2022

My guess is it's something related to the Activity instead of the Fragment. Are you overriding the onActivityResult method in the Activity? That can prevent results from being passed on to the Fragment if it's not done correctly.

@knight109
Copy link
Author

You're right about the error in the activity.
There was a code about onActivityResult, i delete it but i still got the error.
So i just do many thing and ended up find what i should do.

 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;

Thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants