Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Android

Pedro Guimaraes
Pedro Guimaraes
2,671 Points

Handling the back button on a View

Hi, I am currently watching "http://teamtreehouse.com/library/build-a-selfdestructing-message-android-app" and I am on this part (the Extra credit part):

Super Users There is a lot you can do with the ParseUser class. Check out the >documentation and see if you can implement a "Forgot Password?" link on the >login screen following the instructions in the Parse Android Guide.

I am trying to implement the forgot password part but, before actually doing that, I was thinking about how I would set it up on the layout and my idea is: on the login screen whenever the user clicks on "Forgot your password?" the layout fragment changes (without getting me to a new one). I just play around with the objects visibility. It works fine but I have two questions (and one problem): when I hit the back button I am kicked to the Home scren (I wanted to undo the changes made by the ForgotPassword OnClickListener). And is there a way to animate the process?

My LoginActivity.java https://dl.dropboxusercontent.com/u/24445885/LoginActivity.java

My activity_login.xml https://dl.dropboxusercontent.com/u/24445885/activity_login.xml

A video of what is going on https://www.youtube.com/watch?v=PJOOpupJv9w

EDIT: Uploaded new versions of the files

3 Answers

Alessandro Calorì
Alessandro Calorì
10,241 Points

You should handle yourself the onBackPressed event in the activity.

@Override
public void onBackPressed()
{
    if ( <<activity is in password recovery mode>> )
    {
        // Change the layout
    }
    else
    {
        super.onBackPressed();
    }
}
Pedro Guimaraes
Pedro Guimaraes
2,671 Points

It doesn't work. I created a button that is only activated when the user tries to recover the password. Then I did this:

@Override
    public void onBackPressed() {
        if (mEmail.isActivated()) {
            mUsername.setVisibility(View.VISIBLE);
            mPassword.setVisibility(View.VISIBLE);
            mEmail.setVisibility(View.GONE);
            mSendEmailButton.setVisibility(View.GONE);
            mForgotPassword.setVisibility(View.VISIBLE);
            mLoginButton.setVisibility(View.VISIBLE);
            mNoAccount.setVisibility(View.VISIBLE);
            mSignup.setVisibility(View.VISIBLE);
            mRequiredAccount.setVisibility(View.VISIBLE);
        }
        else {
            super.onBackPressed();
        }

Which is the opposite of the Forgot Password button does

And when I press the back button it goes to the home screen :(

Alessandro Calorì
Alessandro Calorì
10,241 Points

Why are you testing if the mEmail View is activated? You should check if it is visible (not activated) or just use a class variable to store the state of the activity.

Try with this:

@Override
    public void onBackPressed() {
        if (mEmail.getVisibility() == View.VISIBLE) {
            mUsername.setVisibility(View.VISIBLE);
            mPassword.setVisibility(View.VISIBLE);
            mEmail.setVisibility(View.GONE);
            mSendEmailButton.setVisibility(View.GONE);
            mForgotPassword.setVisibility(View.VISIBLE);
            mLoginButton.setVisibility(View.VISIBLE);
            mNoAccount.setVisibility(View.VISIBLE);
            mSignup.setVisibility(View.VISIBLE);
            mRequiredAccount.setVisibility(View.VISIBLE);
        }
        else {
            super.onBackPressed();
        }
    }