2015-07-10 60 views
0

我目前在我的主要活動中有一個Facebook登錄按鈕,並且希望用戶在登錄後查看新的片段。但是,在登錄後重新回到主要活動,我不知道爲什麼。我在XML中定義的按鈕:Android中的Facebook登錄按鈕在登錄後將用戶返回到同一頁面

<com.facebook.login.widget.LoginButton 
     xmlns:facebook="http://schemas.android.com/apk/res-auto" 
     facebook:com_facebook_login_text="FACEBOOK" 
     android:id="@+id/login_button" 
     android:layout_width="230dp" 
     android:layout_height="30dp"/> 

,並在我onCreate() MainActivity我初始化按鈕:

fbLogIn = (LoginButton) findViewById(R.id.login_button); 
fbLogIn.setReadPermissions("user_friends"); 
fbLogIn.registerCallback(FbFragment.mCallBackManager, FbFragment.mCallBack); 

FbFragment是我希望用戶通過按鈕登錄後要採取的片段 - 它包含像CallbackManager的東西:

public class FbFragment extends Fragment { 
    private static TextView mTextDetails; 
    public static CallbackManager mCallBackManager = CallbackManager.Factory.create();; 
    public static FacebookCallback<LoginResult> mCallBack = new FacebookCallback<LoginResult>() { 

     @Override 
     public void onSuccess(LoginResult loginResult) { 
      AccessToken accessToken = loginResult.getAccessToken(); 
     } 

     @Override 
     public void onCancel() { 
     } 

     @Override 
     public void onError(FacebookException e) { 
     } 
    }; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

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

     return inflater.inflate(R.layout.login_fragment, container, true); 
    } 

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

我不明白爲什麼這個片段永遠不會顯示。出於絕望,我甚至嘗試在MainActivity中設置一個onClickListener(),其中FbFragment在每次單擊按鈕時被初始化,但這也不起作用(不出所料)。

任何想法?高感謝您的幫助,謝謝

回答

0

我不認爲你在onActivityResult越來越回調()在你的FbFragment請檢查並告訴我們,如果任何異常拋出..

+0

嘿,我該如何檢查? –

+0

調試應用程序或打印日誌onActivityResult()並通過登錄處理... – NilayDani

+0

好吧,事實證明你是對的 - 它永遠不會被調用。我能做些什麼嗎? –

0

這是Facebook登錄活動: - 希望這可以幫助 !!!

public class FacebookActivity extends BaseActivity { 

private String TAG = this.getClass().getSimpleName(); 
private ImageView cancelBtn, iv_googlePlus, loginBtn; 
private String accessToken; 
private String android_id = ""; 
private TextView tv_letsFrndOnFb,tv_knowYouBetter,tv_and,tv_serveYouBetter,tv_willNotPost; 

private Context FbContext = FacebookActivity.this; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_facebook); 

    iv_googlePlus = (ImageView) findViewById(R.id.iv_googlePlus); 
    tv_letsFrndOnFb = (TextView) findViewById(R.id.tv_letsFrndOnFb); 
    tv_knowYouBetter = (TextView) findViewById(R.id.tv_knowYouBetter); 
    tv_and = (TextView) findViewById(R.id.tv_and); 
    tv_serveYouBetter = (TextView) findViewById(R.id.tv_serveYouBetter); 
    tv_willNotPost = (TextView) findViewById(R.id.tv_willNotPost); 
    tv_letsFrndOnFb.setTypeface(PACIFICO); 
    tv_knowYouBetter.setTypeface(PROXIMA_NOVA_BOLD); 
    tv_and.setTypeface(PROXIMA_NOVA_LIGHT_ITALIK); 
    tv_serveYouBetter.setTypeface(PROXIMA_NOVA_BOLD); 
    tv_willNotPost.setTypeface(PROXIMA_NOVA_LIGHT); 

    cancelBtn = (ImageView) findViewById(R.id.iv_cancelBtn); 
    loginBtn = (ImageView) findViewById(R.id.iv_checkBtn); 

    android_id = Settings.Secure.getString(FacebookActivity.this.getContentResolver(), 
      Secure.ANDROID_ID); 
    Log.d(TAG,"DEVICE_ID : "+android_id); 

    cancelBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent intent = new Intent(FacebookActivity.this, NumberVerifyActivity.class); 
      startActivity(intent); 
      FacebookActivity.this.finish(); 
     } 
    }); 

    loginBtn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      if(isNetworkAvailable()) { 
       fbLogin(); 
      } else 
       displayToast(R.string.need_network); 
     } 
    }); 
} 

public void fbLogin() { 
    Session.openActiveSession(FacebookActivity.this, true, 
      new Session.StatusCallback() { 

       // callback when session changes state 
       @SuppressWarnings("deprecation") 
       @Override 
       public void call(Session session, SessionState state, 
           Exception exception) { 

        Log.v(TAG, "APP ID : " + session.getApplicationId()); 

        accessToken = session.getAccessToken(); 

        Log.v(TAG, "ACCESS TOKEN : " + accessToken); 
        if (session.isOpened()) { 
         Log.v(TAG, "SESSION STATE : " + "Active"); 
         Request.executeMeRequestAsync(session, 
           new Request.GraphUserCallback() { 

            @Override 
            public void onCompleted(GraphUser user, 
                  Response response) { 

             System.out.println(TAG 
               + "FACEBOOK RESPONSE : " 
               + response.toString()); 
             GraphObject go = response 
               .getGraphObject(); 
             go.getProperty("gender"); 
             System.out.println(TAG 
               + "USER GENDER : " 
               + go.getProperty("gender")); 
             if (user != null) { 
              System.out.println(TAG 
                + "USER NAME : " 
                + user.getName()); 
              System.out.println(TAG 
                + "USER FACEBOOK ID : " 
                + user.getId()); 
              System.out.println(TAG 
                + "USER FIRST NAME : " 
                + user.getFirstName()); 
              System.out.println(TAG 
                + "USER LAST NAME : " 
                + user.getLastName()); 
              System.out.println(TAG 
                + "USER DOB : " 
                + user.getBirthday()); 
               if(user.getName()!=null && go.getProperty("gender")!=null && user.getId()!=null){ 


              if(user.getName()!=null && go.getProperty("gender")!=null && user.getId()!=null){ 
               Bundle b = new Bundle(); 
               b.putString("Gender", go.getProperty("gender").toString()); 
               Intent i = new Intent(FacebookActivity.this,NumberVerifyActivity.class); 
               i.putExtras(b); 
               startActivity(i); 
              } 
             } 
            } 
           }); 
        } 
       } 
      }); 
} 

@Override 
public void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    Session.getActiveSession().onActivityResult(this, requestCode, resultCode, data); 
} 
} 
相關問題