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);
}
}
初期フラグメント.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;
}
問題は、Session が常に OPENING であるため、メソッド makeMeARequest() が呼び出されないことです...なぜこれが変更されないのです Opening > Opened?? Facebook にログインしてアプリを承認することはできますが、セッションが開かれることはありません。
それは何かが欠けていますか?