1

I have two activity, when I login facebook activity success. It will be auto open activity 2 and tranfer data and picture. But it not working. please help me! The problem is how to call another activity right and transfer data after authorization succeed login facebook button. I've been reading other posts but there is no solution that helped me.

Activity

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        FacebookSdk.sdkInitialize(getApplicationContext());
        setContentView(R.layout.activity_main);
        callbackManager = CallbackManager.Factory.create();
        login = (LoginButton)findViewById(R.id.login_button);
        profile = (ProfilePictureView)findViewById(R.id.picture);
        shareDialog = new ShareDialog(this);
        share = (Button)findViewById(R.id.share);
        details = (Button)findViewById(R.id.details);
        login.setReadPermissions("public_profile email");
        share.setVisibility(View.INVISIBLE);
        details.setVisibility(View.INVISIBLE);
        details_dialog = new Dialog(this);
        details_dialog.setContentView(R.layout.dialog_details);
        details_dialog.setTitle("Details");
        details_txt = (TextView)details_dialog.findViewById(R.id.details);
        email = (TextView) findViewById(R.id.email);
        details.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                details_dialog.show();
            }
        });

        if(AccessToken.getCurrentAccessToken() != null){
            RequestData();
            share.setVisibility(View.VISIBLE);
            details.setVisibility(View.VISIBLE);
        }
        login.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(AccessToken.getCurrentAccessToken() != null) {

                    share.setVisibility(View.INVISIBLE);
                    details.setVisibility(View.INVISIBLE);
//                    profile.setProfileId(null);
//                    email.setText("");

                }

            }
        });
        accessTokenTracker = new AccessTokenTracker() {
            @Override
            protected void onCurrentAccessTokenChanged(AccessToken oldAccessToken,
                                                       AccessToken currentAccessToken) {
                if (currentAccessToken == null) {
                    //write your code here what to do when user logout
                    profile.setProfileId(null);
                    email.setText("");
                }
            }
        };
        share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ShareLinkContent content = new ShareLinkContent.Builder().build();
                shareDialog.show(content);

            }
        });
        login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                if(AccessToken.getCurrentAccessToken() != null){
                    RequestData();
                    share.setVisibility(View.VISIBLE);
                    details.setVisibility(View.VISIBLE);

                }
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                Log.e(TAG, "onError: " + error.getMessage());
            }
        });

    }

    public void RequestData(){
        GraphRequest request = GraphRequest.newMeRequest(AccessToken.getCurrentAccessToken(), new GraphRequest.GraphJSONObjectCallback() {
            @Override
            public void onCompleted(JSONObject object, GraphResponse response) {

                JSONObject json = response.getJSONObject();
                try {
                    if (json != null) {
                        String text = "<b>Name :</b> " + json.getString("name") + "<br><br><b>Email :</b> " + json.getString("email") + "<br><br><b>Profile link :</b> " + json.getString("link");
                        details_txt.setText(Html.fromHtml(text));
                        profile.setProfileId(json.getString("id"));
                        email.setText(json.getString("email"));
                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        Bundle parameters = new Bundle();
        parameters.putString("fields", "id,name,link,email,picture");
        request.setParameters(parameters);
        request.executeAsync();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        callbackManager.onActivityResult(requestCode, resultCode, data);
        Intent intent=new Intent(MainActivity.this,Main2Activity.class);
        String emails=email.getText().toString();
        intent.putExtra("emails", emails);        
        intent.putExtra("BitmapImage", profile);
        startActivity(intent);
    }

}

activiti2

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        username = (TextView) findViewById(R.id.textView);
        imageView = (ImageView) findViewById(R.id.imageView);
        String value=getIntent().getStringExtra("emails");
        username.setText(value);
    }
tony Hoang
  • 33
  • 6

2 Answers2

1

Change your code like this:

login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                if(AccessToken.getCurrentAccessToken() != null){
                    RequestData();
                    share.setVisibility(View.VISIBLE);
                    details.setVisibility(View.VISIBLE);

                }
Intent intent=new Intent(MainActivity.this,Main2Activity.class);
        String emails=email.getText().toString();
        intent.putExtra("emails", emails);        
        intent.putExtra("BitmapImage", profile);
        startActivity(intent);
            }

            @Override
            public void onCancel() {

            }

            @Override
            public void onError(FacebookException error) {
                Log.e(TAG, "onError: " + error.getMessage());
            }
        });



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

I hope this would work.

Amit Tiwari
  • 3,684
  • 6
  • 33
  • 75
0

Start your activity in onSuccess method.

login.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
            @Override
            public void onSuccess(LoginResult loginResult) {

                if(AccessToken.getCurrentAccessToken() != null){
                    RequestData();
                    share.setVisibility(View.VISIBLE);
                    details.setVisibility(View.VISIBLE);

                }
            }
KDeogharkar
  • 10,939
  • 7
  • 51
  • 95
  • thanks, It's working, but can you give me solution tranfer image between 2 activity? – tony Hoang Mar 23 '16 at 09:15
  • you can transfer bitmap between activity because it is parcable object so you can convert your image to bitmap and transfer through activty. – KDeogharkar Mar 23 '16 at 09:16
  • if it is helpful @tonyHoang . Accept my answer. – KDeogharkar Mar 23 '16 at 09:17
  • check http://stackoverflow.com/questions/12908048/passing-bitmap-between-two-activities – KDeogharkar Mar 23 '16 at 09:18
  • @tonyHoang you should not pass a bitmap between two activities. If the size of bitmap is large, your app might crash. Try saving the bitmap to a file in the form of .jpg or .png and pass the filename to the other activity. – Amit Tiwari Mar 23 '16 at 11:57