Home » Android » enableAutoManage() in fragment

enableAutoManage() in fragment

Posted by: admin November 1, 2017 Leave a comment

Questions:

I am following Google’s documentation to implement Google+ Sign In feature into my app.I use Fragments in (login+signup+resetpass) and then User login to main activity!

Error code :

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

this is wrong because it wants an OnConnectionFailedListener object, not an Activity.
So I cast it to OnConncetionFailedListener:

mGoogleApiClient = new GoogleApiClient.Builder(this)
    .enableAutoManage(this, new GoogleApiClient.OnConnectionFailedListener() {
        @Override
        public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
            // connection failed, should be handled
        }
    })
    .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
    .build();

But it doesn’t work I can’t deal with it because when I replace this with getActivity() I have many problem with casting

LoginFragment :

public class LoginFragment extends Fragment implements View.OnClickListener{
private static final int RC_SIGN_IN = 9001;
private GoogleApiClient mGoogleApiClient;
private SignInButton signInButton;
private Button btn_login;
private EditText et_email,et_password;
private TextView tv_register,tv_reset_password;
private ProgressBar progress;
private SharedPreferences pref;


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_login,container,false);
    initViews(view);
    return view;

    GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
            .requestEmail()
            .build();
    mGoogleApiClient = new GoogleApiClient.Builder(this)
            .enableAutoManage(this /* FragmentActivity */, this /* OnConnectionFailedListener */)
            .addApi(Auth.GOOGLE_SIGN_IN_API, gso)
            .build();

}

private void initViews(View view){

    pref = getActivity().getPreferences(0);




    btn_login = (Button)view.findViewById(R.id.btn_login);
    tv_register = (TextView)view.findViewById(R.id.tv_register);
    tv_reset_password = (TextView)view.findViewById(R.id.tv_reset_password);
    et_email = (EditText)view.findViewById(R.id.et_email);
    et_password = (EditText)view.findViewById(R.id.et_password);

    progress = (ProgressBar)view.findViewById(R.id.progress);

    btn_login.setOnClickListener(this);
    tv_register.setOnClickListener(this);
    tv_reset_password.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()){

        case R.id.tv_register:
            goToRegister();
            break;

        case R.id.btn_login:
            String email = et_email.getText().toString();
            String password = et_password.getText().toString();

            if(!email.isEmpty() && !password.isEmpty()) {

                progress.setVisibility(View.VISIBLE);
                loginProcess(email,password);

            } else {

                Snackbar.make(getView(), "empty!", Snackbar.LENGTH_LONG).show();

            }
            break;
        case R.id.tv_reset_password:
            goToResetPassword();
            break;
    }
}
private void loginProcess(String email,String password){

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RequestInterface requestInterface = retrofit.create(RequestInterface.class);


    User user = new User();
    user.setEmail(email);
    user.setPassword(password);
    final ServerRequest request = new ServerRequest();
    request.setOperation(Constants.LOGIN_OPERATION);
    request.setUser(user);
    Call<ServerResponse> response = requestInterface.operation(request);

    response.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, retrofit2.Response<ServerResponse> response) {

            ServerResponse resp = response.body();
            Snackbar.make(getView(), resp.getMessage(), Snackbar.LENGTH_SHORT).show();


            if(resp.getResult().equals(Constants.SUCCESS)){
                SharedPreferences.Editor editor = pref.edit();
                editor.putBoolean(Constants.IS_LOGGED_IN,true);
                editor.putString(Constants.EMAIL,resp.getUser().getEmail());
                editor.putString(Constants.NAME,resp.getUser().getName());
                editor.putString(Constants.UNIQUE_ID,resp.getUser().getUnique_id());
                editor.apply();
                goToProfile();

            }
            progress.setVisibility(View.INVISIBLE);
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {

            progress.setVisibility(View.INVISIBLE);
            Log.d(Constants.TAG,"failed");
            Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();


        }
    });
}

private void goToResetPassword(){

    Fragment reset = new ResetPasswordFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame,reset);
    ft.commit();
}

private void goToRegister(){

    Fragment register = new RegisterFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame,register);
    ft.commit();
}

private void goToProfile(){

    Fragment profile = new ProfileFragment();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame,profile);
    ft.commit();
}

}

MainActivity :

public class MainActivity extends AppCompatActivity {

private SharedPreferences pref;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    pref = getPreferences(0);
    initFragment();
}

private void initFragment(){
    Fragment fragment;
    if(pref.getBoolean(Constants.IS_LOGGED_IN,false)){
        fragment = new ProfileFragment();
    }else {
        fragment = new LoginFragment();
    }
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.fragment_frame,fragment);
    ft.commit();
}

}

What do I do to correct this?
thanks for helping me

Answers: