簡體   English   中英

Android-登錄后Facebook會話永遠不會更改為OPENED

[英]Android - Facebook Session never change to OPENED after Login

我有一個FragmentActivity和另一個具有一些文本和Facebook登錄按鈕的Fragment。

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()從未被調用,因為Session總是OPENING ...為什么不更改Opening> Opened? 我可以進行Facebook登錄並授權我的應用,但會話永遠不會更改為已打開。

它缺少什么嗎?

我已修復它。只需要將所有Facebook代碼移動到我的片段即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM