如何使用嵌套片段的Andr​​oid的Facebook登录 [英] How to login Facebook using nested fragment in Android

查看:246
本文介绍了如何使用嵌套片段的Andr​​oid的Facebook登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我嵌套片段像下面这样。


  • MainActivity

    • 碎裂

      • FragmentA1

        • FragmentA3


      • FragmentA2


    • FragmentB

      • FragmentB1



我想从FragmentA3登录博客。但不能。

在FragmentA3,在onResume我的应用程序停止后调用的onActivityResult。

我应该怎么办?

FragmentA3

 公共类FragmentA3扩展片段{  公共静态最后的字符串标记= FragmentA3.class.getCanonicalName();
  私人UiLifecycleHelper mFbSdkUiHelper;
  私人OnLoggedListener mCallback;
  私人最终名单<串GT;权限;  公共OthersFBLogin(){
      //必需的空公共构造
      权限= Arrays.asList(basic_info,电子邮件);
  }  公共接口OnLoggedListener {
      //回调,通知有关登录成功。
      公共无效onLoginSuccess();
  }  私人最终Session.StatusCallback mSessionCallback =新Session.StatusCallback(){
      @覆盖
      公共无效呼叫(会话的会话,SessionState会状态,异常除外){
        onSessionStateChange(会话,状态除外);
      }
  };  私人无效onSessionStateChange(会话的会话,SessionState会状态,异常除外){
      Log.d(TAGonSessionStateChange);
      如果(state.isOpened()){
          mCallback.onLoginSuccess();
      }否则如果(state.isClosed()){
          如果(会话!= NULL){
              session.closeAndClearTokenInformation();
          }
      }
  }  @覆盖
  公共无效的onCreate(捆绑savedInstanceState){
      Log.d(TAG的onCreate);
      super.onCreate(savedInstanceState);
      mFbSdkUiHelper =新UiLifecycleHelper(getActivity(),mSessionCallback);
      mFbSdkUiHelper.onCreate(savedInstanceState);
  }  @覆盖
  公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
      Log.d(TAGonCreateView);
      查看rootView = inflater.inflate(R.layout.others_fblogin,集装箱,FALSE);
      LoginButton loginButton =(LoginButton)rootView.findViewById(R.id.login_button);
      loginButton.setFragment(本);
      loginButton.setReadPermissions(权限);      返回rootView;
  }  @覆盖
  公共无效onAttach(活动活动){
      Log.d(TAGonAttach);
      super.onAttach(活动);
      尝试{
          mCallback =(OnLoggedListener)活性;
      }赶上(抛出ClassCastException E){
          抛出新ClassCastException异常(activity.toString()
                  +)必须要使用这个片段实施OnLoggedListener
      }
  }  @覆盖
  公共无效onResume(){
      Log.d(TAGonResume);
      super.onResume();
      会话的会话= Session.getActiveSession();
      如果(会话= NULL&放大器;!及(session.isOpened()|| session.isClosed())){
          onSessionStateChange(会话,session.getState(),NULL);
      }
      mFbSdkUiHelper.onResume();
  }  @覆盖
  公共无效onDestroyView(){
      super.onDestroyView();
  }  @覆盖
  公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
      Log.d(TAG的onActivityResult);
      super.onActivityResult(要求code,结果code,数据);
      mFbSdkUiHelper.onActivityResult(要求code,结果code,数据);
  }  @覆盖
  公共无效的onPause(){
      Log.d(TAG的onPause);
      super.onPause();
      mFbSdkUiHelper.onPause();
  }  @覆盖
  公共无效的onDestroy(){
      super.onDestroy();
      mFbSdkUiHelper.onDestroy();
  }  @覆盖
  公共无效的onSaveInstanceState(捆绑outState){
      Log.d(TAG的onSaveInstanceState);
      super.onSaveInstanceState(outState);
      mFbSdkUiHelper.onSaveInstanceState(outState);
  }
}

LogCat中

  D / com.example.sample.FragmentA3:onAttach
D / com.example.sample.FragmentA3:的onCreate
D / com.example.sample.FragmentA3:onCreateView
D / com.example.sample.FragmentA3:onResume
D / dalvikvm:GC_FOR_ALLOC释放764K,10%免费7977K / 8816K,暂停3ms内,共有6毫秒
W / GooglePlayServicesUtil:谷歌Play服务的缺失。
D / com.example.sample.FragmentA3:在onPause
D / com.example.sample.FragmentA3:onSessionStateChange
W / EGL_emulation:eglSurfaceAttrib没有实现
I /编舞:跳过174帧!该应用程序可能会做它的主线程的工作太多了。
D / com.example.sample.MainActivity:的onActivityResult
D / com.example.sample.FragmentA:的onActivityResult
D / com.example.sample.FragmentA3:的onActivityResult
D / com.example.sample.FragmentA3:onResume


解决方案

请尝试此解决方案:

 公共类FragmentA3扩展片段{  @覆盖
  公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
       片段fragmentA1 = this.getParentFragment();
       片段碎裂= fragmentA1.getParentFragment();      loginButton.setFragment(碎裂);
  }  @覆盖
  公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
      Log.d(TAG的onActivityResult);
      super.onActivityResult(要求code,结果code,数据);
      mFbSdkUiHelper.onActivityResult(要求code,结果code,数据);
  }
}公共类FragmentA1扩展片段{  @覆盖
  公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
      如果(mFragmentA3!= NULL)
         mFragmentA3.onActivityResult(要求code,结果code,数据);      super.onActivityResult(要求code,结果code,数据);
  }
}公共类碎裂扩展片段{  @覆盖
  公共无效的onActivityResult(INT申请code,INT结果code,意图数据){
      如果(mFragmentA1!= NULL)
         mFragmentA1.onActivityResult(要求code,结果code,数据);      super.onActivityResult(要求code,结果code,数据);
  }
}

I have nested fragment like the following.

  • MainActivity
    • FragmentA
      • FragmentA1
        • FragmentA3
      • FragmentA2
    • FragmentB
      • FragmentB1

I want to login facebook from FragmentA3. But can not.

In FragmentA3, my app stop in onResume after called onActivityResult.

What should I do?

FragmentA3

public class FragmentA3 extends Fragment {

  public static final String TAG = FragmentA3.class.getCanonicalName();
  private UiLifecycleHelper mFbSdkUiHelper;
  private OnLoggedListener mCallback;
  private final List<String> permissions;

  public OthersFBLogin() {
      // Required empty public constructor
      permissions = Arrays.asList("basic_info", "email");
  }

  public interface OnLoggedListener {
      //Callback to notify about login success.
      public void onLoginSuccess();
  }

  private final Session.StatusCallback mSessionCallback = new Session.StatusCallback() {
      @Override
      public void call(Session session, SessionState state, Exception exception) {
        onSessionStateChange(session, state, exception);
      }
  };

  private void onSessionStateChange(Session session, SessionState state, Exception exception) {
      Log.d(TAG,"onSessionStateChange");
      if (state.isOpened()) {
          mCallback.onLoginSuccess();
      } else if (state.isClosed()) {
          if (session != null) {
              session.closeAndClearTokenInformation();
          }
      }
  }

  @Override
  public void onCreate(Bundle savedInstanceState) {
      Log.d(TAG,"onCreate");
      super.onCreate(savedInstanceState);
      mFbSdkUiHelper = new UiLifecycleHelper(getActivity(), mSessionCallback);
      mFbSdkUiHelper.onCreate(savedInstanceState);
  }

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      Log.d(TAG,"onCreateView");
      View rootView = inflater.inflate(R.layout.others_fblogin, container, false);
      LoginButton loginButton = (LoginButton) rootView.findViewById(R.id.login_button);
      loginButton.setFragment(this);
      loginButton.setReadPermissions(permissions);

      return rootView;
  }

  @Override
  public void onAttach(Activity activity) {
      Log.d(TAG,"onAttach");
      super.onAttach(activity);
      try {
          mCallback = (OnLoggedListener) activity;
      } catch (ClassCastException e) {
          throw new ClassCastException(activity.toString()
                  + " must implement OnLoggedListener in order to use this fragment");
      }
  }

  @Override
  public void onResume() {
      Log.d(TAG,"onResume");
      super.onResume();
      Session session = Session.getActiveSession();
      if (session != null && (session.isOpened() || session.isClosed())) {
          onSessionStateChange(session, session.getState(), null);
      }
      mFbSdkUiHelper.onResume();
  }

  @Override
  public void onDestroyView() {
      super.onDestroyView();
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.d(TAG,"onActivityResult");
      super.onActivityResult(requestCode, resultCode, data);
      mFbSdkUiHelper.onActivityResult(requestCode, resultCode, data);
  }

  @Override
  public void onPause() {
      Log.d(TAG,"onPause");
      super.onPause();
      mFbSdkUiHelper.onPause();
  }

  @Override
  public void onDestroy() {
      super.onDestroy();
      mFbSdkUiHelper.onDestroy();
  }

  @Override
  public void onSaveInstanceState(Bundle outState) {
      Log.d(TAG,"onSaveInstanceState");
      super.onSaveInstanceState(outState);
      mFbSdkUiHelper.onSaveInstanceState(outState);
  }
}

LogCat

D/com.example.sample.FragmentA3  ﹕ onAttach
D/com.example.sample.FragmentA3  ﹕ onCreate
D/com.example.sample.FragmentA3  ﹕ onCreateView
D/com.example.sample.FragmentA3  ﹕ onResume
D/dalvikvm                       ﹕ GC_FOR_ALLOC freed 764K, 10% free 7977K/8816K, paused 3ms, total 6ms
W/GooglePlayServicesUtil         ﹕ Google Play services is missing.
D/com.example.sample.FragmentA3  ﹕ onPause
D/com.example.sample.FragmentA3  ﹕ onSessionStateChange
W/EGL_emulation                  ﹕ eglSurfaceAttrib not implemented
I/Choreographer                  ﹕ Skipped 174 frames!  The application may be doing too much work on its main thread.
D/com.example.sample.MainActivity﹕ onActivityResult
D/com.example.sample.FragmentA   ﹕ onActivityResult
D/com.example.sample.FragmentA3  ﹕ onActivityResult
D/com.example.sample.FragmentA3  ﹕ onResume

解决方案

Please try this solution:

public class FragmentA3 extends Fragment {

  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
       Fragment fragmentA1 = this.getParentFragment();
       Fragment fragmentA = fragmentA1.getParentFragment();

      loginButton.setFragment(fragmentA);
  }

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      Log.d(TAG,"onActivityResult");
      super.onActivityResult(requestCode, resultCode, data);
      mFbSdkUiHelper.onActivityResult(requestCode, resultCode, data);
  }
}

public class FragmentA1 extends Fragment {

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (mFragmentA3 !=null)
         mFragmentA3.onActivityResult(requestCode, resultCode, data);

      super.onActivityResult(requestCode, resultCode, data);
  }
}

public class FragmentA extends Fragment {

  @Override
  public void onActivityResult(int requestCode, int resultCode, Intent data) {
      if (mFragmentA1 !=null)
         mFragmentA1.onActivityResult(requestCode, resultCode, data);

      super.onActivityResult(requestCode, resultCode, data);
  }
}

这篇关于如何使用嵌套片段的Andr​​oid的Facebook登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆