2016-05-27 59 views
0

我已經使用facebook-android-sdk:4.12.0。當我從我的應用程序登錄時,Facebook本機應用程序啓動並顯示用戶名,密碼屏幕。在這裏,我嘗試在Facebook上註冊,它會提供詳細信息併成功註冊並顯示我的主頁。但如果我點擊設備後退按鈕,它不會再次去我的應用。相反,它關閉應用程序。再從啓動器啓動我的應用程序,顯示相同的Facebook主屏幕,但不會去我的應用程序。在Facebook中成功註冊後不返回到我的應用程序

這個問題只發生在三星S4設備。如何解決這個問題。它是我需要做的任何事情嗎?

LoginFragment.java

@Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     rootView = inflater.inflate(R.layout.fragment_login, container, false); 

     callbackManager = CallbackManager.Factory.create(); 


     facebookLoginButton = (RelativeLayout) rootView.findViewById(R.id.facebook_login_button); 

     LoginManager.getInstance().registerCallback(callbackManager, new FacebookCallback<LoginResult>() { 
      @Override 
      public void onSuccess(LoginResult loginResult) { 
       if (loginResult.getAccessToken() != null) { 
        AccessToken.setCurrentAccessToken(loginResult.getAccessToken()); 

        mListener.onFacebookLoginSuccess(); 
       } else { 
        //Facebook access token not available. 
       } 
      } 

      @Override 
      public void onCancel() { 
       Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.login_cancelled_please_try_again), Toast.LENGTH_SHORT).show(); 
      } 

      @Override 
      public void onError(FacebookException error) { 
       Toast.makeText(getActivity(), getActivity().getResources().getString(R.string.login_cancelled_please_try_again), Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     facebookLoginButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       LoginManager.getInstance().logInWithReadPermissions(LoginFragment.this, Arrays.asList("public_profile", "user_friends")); 
      } 
     }); 

     return rootView; 
    } 

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

LoginActivity.java

public class LoginActivity extends FragmentActivity implements LoginFragment.OnLoginFragmentListener { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 
    } 

    @Override 
    public void onFacebookLoginSuccess() { 
     // Launch Main Activity. 
     launchMainActivity(); 
    } 
} 
+0

你能在這裏發佈的代碼 – mdDroid

+0

有你在活動的活動結果返回任何東西@ Ramprasad 。 – Lampard

+0

@mdDroid我在我的問題中添加了代碼 – Ramprasad

回答

0
LoginManager.getInstance().logInWithReadPermissions(LoginFragment.this.. 
//?! probably problem here, must be activity context getActivity() 

實現這一切活動類,然後調用公共方法從您的片段loginning

((YourActivity)getActivity()).yourLoginMethod(); 

還要檢查:

在擴展應用類:

private static AccessTokenTracker mAccessTokenTracker; 

的OnCreate:

FacebookSdk.sdkInitialize(mContext); 
mAccessTokenTracker = new AccessTokenTracker() { 
      @Override 
      protected void onCurrentAccessTokenChanged(
        AccessToken oldAccessToken, 
        AccessToken currentAccessToken) { 
       if(currentAccessToken != null){ 
        SettingsService.settings.facebookToken = currentAccessToken.getToken(); 
        SettingsService.saveSettings(); 
       } 

      } 
     }; 

在清單:

<activity android:name="com.facebook.FacebookActivity" 
      android:configChanges= 
       "keyboard|keyboardHidden|screenLayout|screenSize|orientation" 
      android:theme="@android:style/Theme.Translucent.NoTitleBar" 
      android:label="@string/app_name" /> 


<meta-data 
      android:name="com.facebook.sdk.ApplicationId" 
      android:value="@string/facebook_app_id" /> 

可用性設置:

public class SettingsModel { 
    public String facebookToken = ""; 
    //.... 
} 

和類:

public class SettingsService { 

    private static String KEY = "yourDesiredKey"; 
    private static Context mContext; 

    public static SettingsModel settings; 

    public SettingsService(){ 
     mContext = LastGrabApplication.getLastGrabContext(); 
     //if(settings == null){ 
     settings = getSettings(); 
     //} 
    } 

    public static void saveSettings(){ 
     if(settings == null){ 
      settings = new SettingsModel(); 
     } 
     SharedPreferences.Editor editor = mContext 
       .getSharedPreferences(KEY, Activity.MODE_PRIVATE).edit(); 
     Gson gson = new Gson(); 
     String jsonSettings = gson.toJson(settings); 
     editor.putString("settings", jsonSettings); 
     editor.commit(); 
     settings = getSettings(); 
    } 


    public static SettingsModel getSettings() { 
     SharedPreferences editor = mContext.getSharedPreferences(KEY, 
       Activity.MODE_PRIVATE); 
     try{ 
      String jsonSettings = editor.getString("settings","settings"); 
      Gson gson = new Gson(); 
      settings = gson.fromJson(jsonSettings, SettingsModel.class); 
     }catch (Exception e){ 
      settings = new SettingsModel(); 
     } 

     return settings; 
    } 


} 

請活性關注實施:

public void loginWithFacebook(){ 
    try{ 
     LoginManager.getInstance().logInWithReadPermissions(this, Arrays.asList("public_profile", "user_friends")); 
    }catch(Exception e){} 
} 

,然後從片段致電:

((YourActivity)getActivity()).loginWithFacebook(); 
+0

在上面的代碼中,什麼是SettingsService? – Ramprasad

+0

這是自定義類,以保持更新的Facebook令牌(我將它序列化爲json並在存儲中保存到saveSettings) - 這對於保持令牌始終更新和保存很有用。 –

+0

我更新了該類的答案 - 希望會清楚)) –

相關問題