Michele Lacorte
Michele Lacorte

Reputation: 5363

Activity doesn't refresh layout in onCreate()

I was creating an application with login to Facebook Based on two activities , once logged on to the second step activity and load the info. The problem is that when I load the images with Picasso I just log in twice before the image appears. I put the code for Picasso in onCreate , onStart , onResume but does not load the image at the first login of the second activity. Please help me guys. This is code:

Picasso.with(getApplicationContext()).load(personPhotoUrl).into(avatar);

This is second activity

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if(Start.SignWithFacebook == true)
    {
        name.setText(FacebookLogin.NameFace);
        desc.setText(FacebookLogin.EmailFace);
        try
        {
            Picasso.with(getApplicationContext()).load("https://graph.facebook.com/" + FacebookLogin.idFacebook + "/picture?type=large").into(avatar);
        }catch(Exception e){}
    }
}

This is main activity when call start of second activity:

        public void onClick(View v) {
        if(v.getId() == R.id.authButton) {
            if(SignWithFacebook == true)
            {
            Intent second = new Intent(this, SecondActivity.class);
            startActivity(second);
            this.finish();
            }
            else
            {
            SignWithFacebook = true;
            Intent face = new Intent(this, FacebookLogin.class);
            startActivity(face);
            }
        }

This is FacebookActivity:

    public class FacebookLogin extends Activity {
    private String TAG = "Start";
    public static String NameFace;
    public static String idFacebook;
    public static String EmailFace;
    public static CallbackManager callbackManager;
    private String id;
    private String firstName;
    private String lastName;
    private String email;
    ProfileTracker profileTracker;
    public void onCreate(Bundle savedInstanceState){
          super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        Start.mBut.setReadPermissions(Arrays.asList("public_profile", "email", "user_friends"));
        callbackManager = CallbackManager.Factory.create();
        Start.mBut.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
                @Override
                public void onSuccess(LoginResult loginResult) {
                    GraphRequest.newMeRequest(
                            loginResult.getAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
                                @Override
                                public void onCompleted(JSONObject user, GraphResponse response) {
                                    if (response.getError() != null) {
                                        // handle error
                                    } else {
                                        Start.SignWithFacebook = true;
                                        id = user.optString("id");
                                        firstName = user.optString("first_name");
                                        lastName = user.optString("last_name");
                                        email = user.optString("email");
                                        Log.i(TAG,"User ID "+ id);
                                        EmailFace = "" + email;
                                        Log.i(TAG,"Email "+ email);
                                        Start.text.setText("" + email);
                                        idFacebook = id;
                                    }
                                    NameFace = firstName + " " + lastName;
                                    Toast.makeText(getApplicationContext(), "Log in with " + NameFace, Toast.LENGTH_LONG).show();
                                }
                            }).executeAsync();
            }

                @Override
                public void onCancel() {
                    Start.SignWithFacebook = false;
            }

                @Override
                public void onError(FacebookException exception) {

            }
        });
}

    public void onStart() {
        super.onStart();

    }

     @Override
     public void onActivityResult(int requestCode, int resultCode, Intent data) {
         super.onActivityResult(requestCode, resultCode, data);
     }

     public void onResume()
     {
         super.onResume();
        finish();

     }
}

Upvotes: 0

Views: 226

Answers (2)

Mohamed
Mohamed

Reputation: 864

You must download the image Synchronously:

Bitmap imageBitmap =  Picasso.with(getApplicationContext()).load("https://graph.facebook.com/" + FacebookLogin.idFacebook + "/picture?type=large").get()

And after, you can set the image:

ImageView myImageView = (ImageView) findViewById(R.id.myImageView);
myImageView.setImageBitmap(imageBitmap);

NB: You must use AsyncTask to do this.

Upvotes: 1

Amit K. Saha
Amit K. Saha

Reputation: 5951

As per our discussion with OP in the comments, the image url is wrong, as you are getting "graph.facebook.com/null/picture?type=large" as url at the first time due to FacebookLogin.idFacebook's null value

Upvotes: 0

Related Questions