Challenge :: Announcing Events

Hi @cstewart. I added the following method to my app’s CrimeFragment.java class. I found a bug and I’m not sure how to fix it and would appreciate your help.

Repro Steps:

  1. Turn on Talkback
  2. Run the app
  3. Double tap the + button to create a new crime
  4. Double tap the photo button to go to the camera app
  5. Double tap the back button to return to the CriminalIntent app

Observed Result:

  • User hears the announcement “Crime Scene Photo set”

Expected Result:

  • User doesn’t hear the announcement “Crime Scene Photo set”

|

Snippet from CrimeFragment.java
@Override
public void onResume() {
    super.onResume();
    if (mPhotoView.getDrawable() != null) {
        mPhotoView.announceForAccessibility("Crime Scene Photo set");
    }
}

Hi @cstewart,
I’m stuck trying to implement the 3rd challenge and appreciate some help.
I’m trying to make the call to View.announceForAccessibility in the onResume() method of CrimeFragment.java and it is not working as expected.

Hi asett,

I guess the issue only occurs when the screen is in portrait and the reason is because of the usage of ViewPager.
As you know, ViewPager will create 3 fragments by default (the one you click in the list, the one in the left and the one in the right). All three onResume() methods of each fragment is called even you only click on one of them.
It’s fine to apps without voice. However, it will create an issue when the voice is needed.

I finished the task by referring the above solution. By the way, I put the announceForAccessibility() in the onActivityResult().

The code in CrimeFragment.java is as follows.

    private boolean mIsVisibleToUser;

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);

        mIsVisibleToUser = isVisibleToUser;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_OK) {
            return;
        }

        if ((requestCode == REQUEST_DATE) && (data != null)) {
            // ......
        } else if ((requestCode == REQUEST_CONTACT) && (data != null)) {
            // ......
        } else if (requestCode == REQUEST_PHOTO) {
            Uri targetUri = FileProvider.getUriForFile(getActivity(),
                    "com.star.criminalintent.fileprovider", mPhotoFile);

            getActivity().revokeUriPermission(targetUri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);

            updatePhotoView();
        }

    private void updatePhotoView() {
        if ((mPhotoFile == null) || !mPhotoFile.exists()) {
            // ......
            if (mIsVisibleToUser) {
                mPhotoView.announceForAccessibility(
                        getResources().getString(R.string.crime_photo_no_image_description));
            }
        } else {
            // ......
            if (mIsVisibleToUser) {
                mPhotoView.announceForAccessibility(
                        getResources().getString(R.string.crime_photo_image_description));
            }
        }
    }
1 Like

Thanks @crystalDf,
I will check this out and let you know how it goes