我寫了一個Fragment
來管理Facebook登錄/登出Facebook Android SDK 3.5.2,下面的例子是here。Facebook Android SDK:爲什麼在調用Fragment的onActivityResult之前重新創建Activity?
當通過Facebook Web部件的登錄過程完成後,MainActivity
再次啓動(調用onCreate
)。
然後在如果savedInstanceState!=null
在MainActivity
的onCreate
,則savedInstanceState
檢查或知道如果現有FacebookPrefsFragment
實例必須恢復,如果必須創建一個新的實例示例代碼:
if (savedInstanceState == null) {
// Add the fragment on initial activity setup
mainFragment = new MainFragment();
getSupportFragmentManager()
.beginTransaction()
.add(android.R.id.content, mainFragment)
.commit();
} else {
// Or set the fragment from restored state info
mainFragment = (MainFragment) getSupportFragmentManager()
.findFragmentById(android.R.id.content);
}
我的問題是我的MainActivity
有很多Fragments
,並且savedInstanceState
不僅可以在Facebook登錄過程中不爲空。所以我有兩個問題:
爲什麼在Facebook登錄過程後再次創建
MainActivity
?不應該僅僅調用FacebookPrefsFragment
的onActivityResult
?如果它將被創建,我怎麼能知道這是因爲Facebook SDK調用它?
MainActivity
public class MainActivity extends FragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
// If the Activity has been started after the Facebook login process, restart the FacebookPrefsFragment,
// but how can I be sure that MainActivity has been called by the Facebook app?
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
Fragment facebookPrefsFragment = new FacebookPrefsFragment();
fragmentTransaction.replace(R.id.content_container, facebookPrefsFragment, "facebookPrefsFragment");
fragmentTransaction.commit();
}
else{
// start other Fragment
}
}
// ...
}
FacebookPrefsFragment
public class FacebookPrefsFragment extends Fragment{
private void onSessionStateChange(Session session, SessionState state, Exception exception) {
if (state.isOpened()) {
Log.i(this.getClass().getSimpleName(), "Logged in", MyApplication.LOG_LEVEL);
} else if (state.isClosed()) {
Log.i(this.getClass().getSimpleName(), "Logged out", MyApplication.LOG_LEVEL);
}
}
private Session.StatusCallback callback = new Session.StatusCallback() {
@Override
public void call(Session session, SessionState state, Exception exception) {
onSessionStateChange(session, state, exception);
}
};
private UiLifecycleHelper uiHelper;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.facebook_prefs, container, false);
LoginButton authButton = (LoginButton) view.findViewById(R.id.authButton);
authButton.setPublishPermissions(Arrays.asList("publish_actions"));
authButton.setFragment(this);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
uiHelper = new UiLifecycleHelper(getActivity(), callback);
uiHelper.onCreate(savedInstanceState);
}
@Override
public void onResume() {
super.onResume();
Session session = Session.getActiveSession();
if (session != null &&
(session.isOpened() || session.isClosed())) {
onSessionStateChange(session, session.getState(), null);
}
uiHelper.onResume();
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
uiHelper.onActivityResult(requestCode, resultCode, data);
}
@Override
public void onPause() {
super.onPause();
uiHelper.onPause();
}
@Override
public void onDestroy() {
super.onDestroy();
uiHelper.onDestroy();
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
uiHelper.onSaveInstanceState(outState);
}
}
你能給如何Facebook的SDK將開始您的活動的例子嗎? –
使用我的問題中指出的代碼,當登錄完成後,調用Facebook Android本機應用程序中設置的「Class Name」(請參閱https://developers.facebook.com/docs/android/的第5部分開始/ facebook-sdk-for-android /) – jul
你的意思是與facebook的深層鏈接?這是你做的一個方法:https://developers.facebook.com/docs/android/link-to-your-native-app/#step7 –