2013-10-30 54 views
0

我有一個FragmentActivity和另一個片段,有一些文本和Facebook登錄按鈕。Android - Facebook會話永遠不會改變到打開後

main.xml中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 

<fragment 
    android:id="@+id/initialFragment" 
    android:name="com.tests.android.InitialFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 

<fragment 
    android:id="@+id/loginFragment" 
    android:name="com.tests.android.LoginFragment" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" /> 
</LinearLayout> 

初始片段

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:fillViewport="true" > 
... 
     <com.facebook.widget.LoginButton 
      android:id="@+id/loginButtonFacebook" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_marginBottom="110dp" /> 
... 
</ScrollView> 

而且我的java類:

InitialActivity.java

public class InitialActivity extends FragmentActivity { 

public static final int INITIAL = 0; 
public static final int LOGIN_APP = 1; 
public static final int SIGNIN_APP = 2; 
private static final int FRAGMENT_COUNT = SIGNIN_APP + 1; 

public int ACTIVE_FRAGMENT = -1; 

private static final int REAUTH_ACTIVITY_CODE = 100; 

private Fragment[] fragments = new Fragment[FRAGMENT_COUNT]; 

private boolean isResumed = false; 

private UserHelper userHelper = null; 

private UiLifecycleHelper uiHelper; 
private Session.StatusCallback callback = new Session.StatusCallback() { 
    @Override 
    public void call(Session session, SessionState state, 
      Exception exception) { 
     onSessionStateChange(session, state, exception); 
    } 
}; 

public Session.StatusCallback getCallback() { 
    return callback; 
} 


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

    uiHelper = new UiLifecycleHelper(this, callback); 
    uiHelper.onCreate(savedInstanceState); 

    userHelper = new UserHelper(getApplicationContext()); 

    setContentView(R.layout.main); 

    FragmentManager fm = getSupportFragmentManager(); 
    //fm.addOnBackStackChangedListener(getListener()); 
    fragments[INITIAL] = fm.findFragmentById(R.id.initialFragment); 
    fragments[LOGIN_APP] = fm.findFragmentById(R.id.loginFragment); 
    fragments[SIGNIN_APP] = fm.findFragmentById(R.id.profileFragment); 

    FragmentTransaction transaction = fm.beginTransaction(); 
    for (int i = 0; i < fragments.length; i++) { 
     transaction.hide(fragments[i]); 
    } 
    transaction.commit();  
} 

public void showFragment(int fragmentIndex, boolean addToBackStack) { 
    FragmentManager fm = getSupportFragmentManager(); 
    FragmentTransaction transaction = fm.beginTransaction(); 
    for (int i = 0; i < fragments.length; i++) { 
     if (i == fragmentIndex) { 
      transaction.show(fragments[i]); 
     } else { 
      transaction.hide(fragments[i]); 
     } 
    } 
    if (addToBackStack) { 
     transaction.addToBackStack(null); 
    } 
    transaction.commit(); 
} 

@Override 
public void onResume() { 
    super.onResume(); 
    uiHelper.onResume(); 
    isResumed = true; 
} 

@Override 
public void onPause() { 
    super.onPause(); 
    uiHelper.onPause(); 
    isResumed = false; 
} 

@Override 
public void onDestroy() { 
    super.onDestroy(); 
    uiHelper.onDestroy(); 
} 

@Override 
protected void onSaveInstanceState(Bundle outState) { 
    super.onSaveInstanceState(outState); 
    uiHelper.onSaveInstanceState(outState); 
} 


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


} 

public void onSessionStateChange(Session session, SessionState state, 
     Exception exception) { 
    // Only make changes if the activity is visible 
    if (isResumed) { 
     FragmentManager manager = getSupportFragmentManager(); 
     // Get the number of entries in the back stack 
     int backStackSize = manager.getBackStackEntryCount(); 
     // Clear the back stack 
     for (int i = 0; i < backStackSize; i++) { 
      manager.popBackStack(); 
     } 

     if (state.isOpened()) { 
      makeMeRequest(session); 
      Intent intent = new Intent(getApplicationContext(), OptionsActivity.class); 
      startActivity(intent); 
      finish(); 
     } else if (state.isClosed()) { 
      showFragment(INITIAL, false); 
     } 
    } 
} 

private void makeMeRequest(final Session session) { 
    // Make an API call to get user data and define a 
    // new callback to handle the response. 
    Request request = Request.newMeRequest(session, 
      new Request.GraphUserCallback() { 
     @Override 
     public void onCompleted(GraphUser graphUser, Response response) { 
      // If the response is successful 
      if (session == Session.getActiveSession()) { 
       if (graphUser != null) { 
        InitialActivity.this.persistUser(graphUser, session); 
       } 
      } 
      if (response.getError() != null) { 
       // Handle errors, will do so later. 
      } 
     } 
    }); 
    request.executeAsync(); 
} 

@Override 
protected void onResumeFragments() { 
    super.onResumeFragments(); 
    if(ACTIVE_FRAGMENT == -1){ 
     Session session = Session.getActiveSession();  
     Boolean isLogged = userHelper.isLogged();  
     if ((session != null && session.isOpened()) || isLogged.equals(Boolean.TRUE)) { 
      makeMeRequest(session); 
      Intent intent = new Intent(getApplicationContext(), OptionsActivity.class); 
      startActivity(intent); 
      finish(); 
     } else { 
      showFragment(INITIAL, false); 
     } 
    } else { 
     this.showFragment(ACTIVE_FRAGMENT, true); 
    }  
} 

InitialFragment.java

public class InitialFragment extends Fragment implements CustomFragmentOperations { 


@Override 
public View onCreateView(LayoutInflater inflater, 
     ViewGroup container, Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.initial, 
      container, false); 

    final InitialActivity mainActivity = ((InitialActivity)getActivity()); 
    LoginButton loginButtonFacebook = (LoginButton) view 
      .findViewById(R.id.loginButtonFacebook); 
    loginButtonFacebook.setFragment(this); 

    loginButtonFacebook.setSessionStatusCallback(mainActivity.getCallback()); 


    /*loginButtonFacebook.setReadPermissions(Arrays.asList("user_likes", 
      "user_status", "email", "basic_info"));*/ 

    Button buttonExit = (Button) view.findViewById(R.id.buttonExit); 
    buttonExit.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      mainActivity.finish(); 
     } 
    }); 

    Button buttonSignIn = (Button) view.findViewById(R.id.buttonSignIn); 
    buttonSignIn.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View view) {     
      mainActivity.ACTIVE_FRAGMENT = InitialActivity.SIGNIN_MOMNT; 
      mainActivity.showFragment(InitialActivity.SIGNIN_MOMNT, true); 
     } 
    }); 

    Button buttonLoginApp = (Button) view.findViewById(R.id.buttonLoginApp); 
    buttonLoginApp.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View arg0) { 
      mainActivity.ACTIVE_FRAGMENT = InitialActivity.LOGIN_MOMNT; 
      mainActivity.showFragment(InitialActivity.LOGIN_MOMNT, true); 
     } 
    }); 

    return view; 
} 


的問題是方法makeMeARequest()不會被調用,因爲會議是始終在打開......這是爲什麼不變更後的打開>打開?我可以做Facebook登錄並授權我的應用程序,但會話永遠不會更改爲打開。

它缺少一些東西嗎?

+0

我已經修復...只需要將所有的Facebook代碼移動到我的片段!任何人都知道當登錄返回到我的活動時調用的方法是什麼? – placplacboom

回答

0

我已經修復它..只需要將所有的facebook代碼移動到我的片段。

+1

提出一個新問題,而不是將您的後續問題放在答案中 – krsteeve

相關問題