Why is my login for Android and Android sdk called twice?

I work with the SDK for Facebook, the problem is that the FormSubmit function runs twice. It is called by the getView function, which is called onCreateView and statusCallback , how to fix it?

 public class Home extends Fragment implements LoginListener { public View rootView; public ImageView HomeBg; public ImageView buttonLoginLogout; public TextView chi; public TextView eng; public ColorStateList oldColor; public SharedPreferences prefs; public EasyTracker tracker = null; //Facebook login private Session.StatusCallback statusCallback = new SessionStatusCallback(); @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { tracker = EasyTracker.getInstance(getActivity()); prefs = getActivity().getSharedPreferences("memberData", 0); getActivity().getActionBar().hide(); rootView = inflater.inflate(R.layout.home, container, false); buttonLoginLogout = (ImageView) rootView.findViewById(R.id.home_connectFB); eng = (TextView) rootView.findViewById(R.id.btn_eng); chi = (TextView) rootView.findViewById(R.id.btn_chi); if (Utility.getLocale(getActivity()).equals("TC")) { chi.setTextColor(getActivity().getResources().getColor( android.R.color.white)); oldColor = eng.getTextColors(); } else { eng.setTextColor(getActivity().getResources().getColor( android.R.color.white)); oldColor = chi.getTextColors(); } eng.setOnClickListener(setChangeLangListener("EN")); chi.setOnClickListener(setChangeLangListener("TC")); //Facebook login Settings.addLoggingBehavior(LoggingBehavior.INCLUDE_ACCESS_TOKENS); Session session = Session.getActiveSession(); if (session == null) { if (savedInstanceState != null) { session = Session.restoreSession(getActivity(), null, statusCallback, savedInstanceState); } if (session == null) { session = new Session(getActivity()); } Session.setActiveSession(session); if (session.getState().equals(SessionState.CREATED_TOKEN_LOADED)) { session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback)); } } updateView(); return rootView; } @Override public void onStart() { super.onStart(); Session.getActiveSession().addCallback(statusCallback); tracker.set(Fields.SCREEN_NAME, "Landing Page " + Utility.getLocale(getActivity())); tracker.send(MapBuilder.createAppView().build()); } @Override public void onStop() { super.onStop(); Session.getActiveSession().removeCallback(statusCallback); EasyTracker.getInstance(getActivity()).activityStop(getActivity()); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); Session.getActiveSession().onActivityResult(getActivity(), requestCode, resultCode, data); } @Override public void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); Session session = Session.getActiveSession(); Session.saveSession(session, outState); } private void updateView() { Session session = Session.getActiveSession(); if (session.isOpened()) { // get request if (!session.getAccessToken().equals(prefs.getString("token", ""))) new FormSubmit(getActivity(),this,tracker).execute("login", session.getAccessToken()); else onTaskComplete(prefs.getString("token", ""),prefs.getString("memberId", "")); } else { buttonLoginLogout.setImageResource(R.drawable.landing_btn_connect_facebook); buttonLoginLogout.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { onClickLogin(); } }); } } private void onClickLogin() { Session session = Session.getActiveSession(); if (!session.isOpened() && !session.isClosed()) { session.openForRead(new Session.OpenRequest(this).setCallback(statusCallback)); } else { Session.openActiveSession(getActivity(), this, true, statusCallback); } } private class SessionStatusCallback implements Session.StatusCallback { @Override public void call(Session session, SessionState state, Exception exception) { updateView(); } } public OnClickListener setChangeLangListener(final String lang) { OnClickListener changeLangListener = new OnClickListener() { @Override public void onClick(View arg0) { Configuration config = new Configuration(getResources().getConfiguration()); if (Utility.getLocale(getActivity()).equals("TC") && lang.equals("EN")) { tracker.send(MapBuilder.createEvent("menu_click","language", "switchEN", null).build()); config.locale = Locale.ENGLISH; chi.setTextColor(oldColor); eng.setTextColor(getActivity().getResources().getColor( android.R.color.white)); } else if (Utility.getLocale(getActivity()).equals("EN") && lang.equals("TC")) { tracker.send(MapBuilder.createEvent("menu_click","language", "switchTC", null).build()); config.locale = Locale.TRADITIONAL_CHINESE; eng.setTextColor(oldColor); chi.setTextColor(getActivity().getResources().getColor( android.R.color.white)); } getResources().updateConfiguration(config,getResources().getDisplayMetrics()); onConfigurationChanged(config); } }; return changeLangListener; } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); Intent intent = getActivity().getIntent(); intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); getActivity().finish(); startActivity(intent); } @Override public void onResume() { super.onResume(); AppEventsLogger.activateApp(getActivity(),getResources().getString(R.string.app_id)); } @Override public void onTaskComplete(String token, String memberId) { Toast.makeText(getActivity(), "t:" + token + "m:" + memberId, Toast.LENGTH_LONG).show(); buttonLoginLogout.setImageResource(R.drawable.landing_btn_take_a_selfie); buttonLoginLogout.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { ((LandingPage)getActivity()).tabHost.setCurrentTab(2); } }); } @Override public void onTaskFailure(String errorMsg) { Toast.makeText(getActivity(), errorMsg, Toast.LENGTH_LONG).show(); } public void saveMemberInfo(String token, String memberId){ SharedPreferences.Editor editor = prefs.edit(); editor.putString("token", token); editor.putString("memberId", memberId); editor.commit(); } } 
+7
android callback facebook android-fragments
source share
3 answers

Update your SessionStatusCallback () method as shown below and check the output.

And if it works, mark it as an accepted answer.

 private class SessionStatusCallback implements Session.StatusCallback { @Override public void call(Session session, SessionState state, Exception exception) { if (session.isOpened()) { updateView(); } } } 

SessionStatusCallback() method will be called every time your session changes from one state to another.

Before you log into Facebook, your SessionState will be SessionState.CLOSED . When you try to get a Facebook login and the authentication step succeeds, your session will change to SessionState.OPENING , and this time your SessionStatusCallback() method will be executed.

But in the current situation, your session does not open completely, it is in the process of opening. So after that, when the session is fully opened, the session state has changed to SessionState.OPENED , and so your SessionStatusCallback() method is called again.

This is why you changed your SessionStatusCallback() method as I described above and it will work just fine.

+5
source share

You are trying to restore / open a session in onCreateView() . Therefore, when the session is ready, a callback is called, therefore updateView() called.

A quick fix is ​​to call updateView() onResume() instead of onCreateView() . Then, inside updateView() , immediately and immediately return if isAdded() returns false . Because in this case updateView() is called by your callback at the moment when the views are not ready. Please take UserSettingsFragment in the FB SDK as an example: https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/widget/UserSettingsFragment.java#L370 .

Also, I would try loading session onActivityCreated() instead of onCreateView() to make sure the activity is created and ready. https://github.com/facebook/facebook-android-sdk/blob/master/facebook/src/com/facebook/widget/FacebookFragment.java

+1
source share

I think you are doing the same request twice.

Delete

 updateView(); 

From onCreate, as it will be called anyway when statusCallback is triggered

0
source share

All Articles