I wrote a Fragment
to manage Facebook log in/out with Facebook Android SDK 3.5.2, following the example given here.
When the login process through Facebook web widgets is done, MainActivity
is started again (onCreate
is called).
Then in the example code if savedInstanceState!=null
in MainActivity
's onCreate
, the savedInstanceState
is checked to know if the existing FacebookPrefsFragment
instance must be restored or if a new instance must be created:
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);
}
My problem is that my MainActivity
has many Fragments
, and savedInstanceState
can be not null not only when on the Facebook login process. So I have two questions:
Why is MainActivity
created again after the Facebook login process? Shouldn't just FacebookPrefsFragment
's onActivityResult
be called?
If it's going to be created, how can I know that it is because the Facebook SDK called it?
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);
}
}
Oops, I found out why the MainActivity
is recreated everytime: that's because I have the option "Don't keep activities" activated on my phone! Now I have to find out what to do in that case...