material-components/material-components-android-codelabs

Next button listener explanation is in Java

LesserCure opened this issue · 4 comments

https://codelabs.developers.google.com/codelabs/mdc-101-kotlin/#4

After the code snippet for view.next_button.setOnClickListener, it says:

We've added the line ((NavigationHost) getActivity()).navigateTo(new ProductGridFragment(), false); to the else case of the click listener.

This sentence should be changed to quote the line from the Kotlin code instead:

We've added the line (activity as NavigationHost).navigateTo(ProductGridFragment(), false) to the else case of the click listener.

This has been updated in the 101 Kotlin codelab. Thanks for pointing this out!

Is there a reason why my code ain`t working:
when I include this code in the Fragment the app fails to launch... but when I comment it out the app launches

` final TextInputLayout passwordTextInput = view.findViewById(R.id.password_text_input);
final TextInputLayout passwordEditText = view.findViewById(R.id.password_edit_text);
MaterialButton nextButton = view.findViewById(R.id.next_button);

    // Snippet from "Navigate to the next Fragment" section goes here.

    nextButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (!isPasswordValid((Editable) passwordEditText.getEditText()))
            {
                passwordTextInput.setError(getString(R.string.shr_error_password));
            }
            else
            {
                passwordTextInput.setError(null);//clear the error

                ((NavigationHost)getActivity()).navigateTo(new ProductGridFragment(),false);// false removes previous screen from back stack
            }
        }
    });

    // Clear the error once more than 8 characters are typed
    passwordEditText.setOnKeyListener(new View.OnKeyListener() {
        @Override
        public boolean onKey(View v, int keyCode, KeyEvent event) {

            if (isPasswordValid((Editable) passwordEditText.getEditText()))
            {
                passwordTextInput.setError(null);// clear the error
            }
            return false;
        }
    });`

am i doing something wrong...

I am surprised that there I cannot use Intent method when want to navigate.

kdb13 commented

I am surprised that there I cannot use Intent method when want to navigate.

Intents are for navigating between activities. Here, we are using a single activity architecture where multiple fragments are hosted inside a single activity. And, to navigate fragments we must use FragmentManager and not Intent.