Facebook ShareDialog: back button doesn't catch by Activity - android

I follow tutorial for write ShareDialog of facebook api, but it doesn't work. When activity is launched, after setting up environment, i show automatically ShareDialog. If i press back button when that dialog is showed, my app close dialog, and open it again. It seems that back button isn't catch by activity. This is my code:
public class ShareScoreOnFb extends FragmentActivity {
private static UiLifecycleHelper uiHelper;
private static Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_score_on_fb);
activity = this;
uiHelper = new UiLifecycleHelper(this, null);
uiHelper.onCreate(savedInstanceState);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data,
new FacebookDialog.Callback() {
#Override
public void onComplete(PendingCall pendingCall, Bundle data) {
String gesture = FacebookDialog
.getNativeDialogCompletionGesture(data);
if (gesture != null) {
if ("post".equals(gesture)) {
Toast.makeText(ShareScoreOnFb.this,
"success",
Toast.LENGTH_SHORT).show();
}
ShareScoreOnFb.this.finish();
}
ShareScoreOnFb.this.finish();
}
#Override
public void onError(PendingCall pendingCall,
Exception error, Bundle data) {
Toast.makeText(ShareScoreOnFb.this, error.toString(),
Toast.LENGTH_SHORT).show();
ShareScoreOnFb.this.finish();
}
});
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
#Override
protected void onResume() {
super.onResume();
uiHelper.onResume();
share();
}
#Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
#Override
public void onBackPressed() {
Log.i("TAG", "backpressed");
super.onBackPressed();
}
#Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
private void share() {
Intent intent = this.getIntent();
int myScore = intent.getIntExtra("MYSCORE", -1);
int oppScore = intent.getIntExtra("OPPONENT_SCORE", -1);
FacebookDialog shareDialog = new FacebookDialog.ShareDialogBuilder(this)
.setName("Score").setLink("http://www.google.it")
.setDescription(myScore + " " + oppScore)
.setPicture("http://www.cs.cmu.edu/~junggon/tools/gear.png")
.build();
uiHelper.trackPendingDialogCall(shareDialog.present());
}
}
I can't find any solution, and i'm going to be crazy!

When you call the native share dialog (in FacebookDialog), it will start another activity (call it Activity Foo) in the Facebook app. So if you click on the back button, it's Activity Foo that catches the back button, and finishes Activity Foo. I believe here's the sequence of events:
Your ShareScoreOnFb activity calls shareDialog.present()
Activity Foo starts, and shows the share dialog
You click back
Activity Foo finishes, and calls your callback (in onActivityResult)
once onActivityResult completes, your ShareScoreOnFb activity's onResume is called, and it will try to share() again

This will handle result without back press.
I have tried this and got success.
On Click (For Link Sample (You can modify your content)) :
shareDialog = new ShareDialog(MainActivity.this);
ShareLinkContent linkContent = new ShareLinkContent.Builder()
.setContentTitle("Hello Facebook")
.setContentDescription("The 'Hello Facebook' sample showcases simple Facebook integration")
.setContentUrl(Uri.parse("http://developers.facebook.com/android"))
.build();
shareDialog.show(linkContent, ShareDialog.Mode.AUTOMATIC);
In onActivityResult method :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

Related

How to start a new activity for putting facebook logout button?

I'm new in Android programming and I don't know how I can put the facebook logout botton in another activity (called whatToDo) considering that the login botton is in the LoginActivity.
This is the initial code of Facebook tutorial to implement Facebook login, but from this code I don't know how to implement what I want.
public class LoginActivity extends Activity {
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
callbackManager = CallbackManager.Factory.create();
TextView login_text = (TextView) findViewById(R.id.fb_login);
LoginButton fbLoginButton = (LoginButton) findViewById(R.id.fb_login_button);
fbLoginButton.setReadPermissions("email");
fbLoginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Intent intent = new Intent(LoginActivity.this, whatToDo.class);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Logged!", Toast.LENGTH_LONG).show();
}
#Override
public void onCancel() {
Toast.makeText(getApplicationContext(), "Cancel Event!", Toast.LENGTH_LONG).show();
}
#Override
public void onError(FacebookException exception) {
Toast.makeText(getApplicationContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
callbackManager.onActivityResult(requestCode, resultCode, data);
super.onActivityResult(requestCode, resultCode, data);
}
}
What extra code do I have to insert in LoginActivity and whatToDo activity?
Thanks in advance!

Android Facebook parse login logic called twice

So here is the code
public void redirectToMain(){
Intent intent = new Intent(getApplicationContext(), MainScreen.class);
startActivity(intent);
finish();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pers.clear();
pers.add("user_friends");
pers.add("email");
if (ParseUser.getCurrentUser() != null) {
redirectToMain();
}
loginButton = (LoginButton) findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.setReadPermissions("email", "user_friends");
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
parseLogin();
}
#Override
public void onCancel() {
// App code
}
#Override
public void onError(FacebookException exception) {
// App code
}
});
}
public void parseLogin(){
loginButton.setVisibility(View.INVISIBLE);
ParseFacebookUtils.logInWithReadPermissionsInBackground(this, pers, new LogInCallback() {
#Override
public void done(ParseUser user, ParseException e) {
if(user==null){
Log.i("Cancel", "facebook");
}else if(user.isNew()){
Log.i("Parse", "succcess");
redirectToMain();
}
else{
Log.i("Parse", "old");
redirectToMain();
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data );
ParseFacebookUtils.onActivityResult(requestCode,resultCode,data);
}
}
And everything works fine, the user gets redirected to the second activity, but the activity gets called twice. Also, the log part that says old is also written twice in the logcat, so my assumption is that either parseLogin() or the ParseFacebookUtils.log...() is called twice. Any idea why that is happening?
I managed to fix it by adding an int and a while loop that executes the code only once. It's more of a hack than a fix, so maybe somebody can come up with a better solution.

Pressing Facebook Login button does nothing

Here is how I implemented callback mechanism for FB login button:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FacebookSdk.sdkInitialize(getApplicationContext());
AppEventsLogger.activateApp(this);
setContentView(R.layout.activity_main);
loginButton = (LoginButton)findViewById(R.id.login_button);
callbackManager = CallbackManager.Factory.create();
loginButton.registerCallback(callbackManager, new FacebookCallback<LoginResult>() {
#Override
public void onSuccess(LoginResult loginResult) {
Log.d("FB onSuccess 1", "");
}
#Override
public void onCancel() {
Log.d("FB onCancel 1", "");
}
#Override
public void onError(FacebookException e) {
Log.d("FB onError 1", "");
}
});
}
When pressing button a spinner appear, starts, but the confirmation screen does not appear, and no log messages are filled into Activity Monitor. What is the problem?
Try Adding this in your Activity :
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
}

How I get the response of a native activity?

For example: my app calls a native settings activity, by
Intent myIntent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(myIntent, CODE);
Then when it is finished (by pressing the back button for example) I want to know. I tried this way, but the onActivityResult() wasn't called when the native activity was finalized... so my app wasn't informed...
How I have to do? Anyone knows?
Thanks...
EDIT:
Thanks every one.
The complete code is that:
public class MainActivity extends AppCompatActivity {
public static final int DIALOG_CODE = 0x1;
private TextView txtGps;
private TextView txtNet;
private LocationManager manager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
first();
}
private void checkConfigs() {
boolean gps = manager.isProviderEnabled(LocationManager.GPS_PROVIDER);
boolean net = manager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
setWidgets(gps, net);
if (gps && net){
Toast.makeText(this, "Configurações Ok!", Toast.LENGTH_SHORT).show();
} else {
DialogConfig.exibDialog(getFragmentManager());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if ((requestCode == DIALOG_CODE) && (resultCode == RESULT_OK)){
checkConfigs();
} else {
Toast.makeText(this, "As config não foram alteradas!", Toast.LENGTH_SHORT).show();
}
}
public void setWidgets(boolean gsp, boolean net){
if (gsp){
txtGps.setText(R.string.gps_hab);
} else {
txtGps.setText(R.string.gps_n_hab);
}
if (net){
txtNet.setText(R.string.wifi_hab);
} else {
txtNet.setText(R.string.wifi_n_hab);
}
}
private View.OnClickListener onClickCheck() {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
checkConfigs();
}
};
}
#SuppressWarnings("ConstantConditions")
private void first() {
txtGps = (TextView) findViewById(R.id.txt_gps);
txtNet = (TextView) findViewById(R.id.txt_net);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
findViewById(R.id.btn_check).setOnClickListener(onClickCheck());
}
public static class DialogConfig extends DialogFragment {
public static void exibDialog(FragmentManager fm){
new DialogConfig().show(fm, "dialog");
}
#Override
public android.app.Dialog onCreateDialog(Bundle savedInstanceState) {
return new AlertDialog.Builder(getActivity())
.setTitle("As conf não estão corretas! Alt configs?")
.setPositiveButton("Ok", onOk())
.setNegativeButton("Cancel", onCancel())
.create();
}
private DialogInterface.OnClickListener onCancel() {
return new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dismiss();
}
};
}
private DialogInterface.OnClickListener onOk() {
return new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent myIntent = new Intent( Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivityForResult(myIntent, DIALOG_CODE);
}
};
}
}
}
[Resolved]
Thanks again to every one, I tried again, creating a interface of callback in dialog, so the activity could be informed and call the other native settings activity by itself , and got the response in onActivityResult(), but comparing only a code (not by RESULT_OK), This way worked. But there is a other way (like was suggested) that use the lifecycle, to me seems more easy.
I basically tried what you tried and it works for me.
startActivityForResult(new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS), 1);
and for onActivityResult:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.d(PREFIX, "onActivityResult");
}
When I came back to the app, onActivityResult was called.
Not sure what you are doing different. can you show your onActivityResult code?

Android FB Connect

I am trying to integrate the official FB SDK for android.
below is the basic code
public class FbTestActivity extends Activity {
Facebook facebook = new Facebook("112374492201730");
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
facebook.authorize(this, new String[] { "email", "read_stream" },
new DialogListener() {
public void onComplete(Bundle values) {
}
public void onFacebookError(FacebookError error) {
}
public void onError(DialogError e) {
}
public void onCancel() {
}
});
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
facebook.authorizeCallback(requestCode, resultCode, data);
}
}
When i open the app the Login or (if looged in) Permission dialog never shows up. Some dialog opens for a few mili secs and closes down.
The problem is the SSO (Single Sign On) feature of the login.
Following link will help you.
facebook login dialog disappears soon after loading
http://ukgupta.blogspot.com/2011/07/facebook-implementation-into-android.html

Categories

Resources