Java 使用facebook和动态片段登录

Java 使用facebook和动态片段登录,java,android,facebook,android-fragments,fragment,Java,Android,Facebook,Android Fragments,Fragment,我有一个应用程序,你需要登录,然后你会去另一个片段。 现在我看到的所有教程都是静态片段,我不想这样,因为我需要刷新片段。现在,因为我没有找到任何关于facebook登录的好的动态片段教程,所以我只想在这里问一下。有人能帮我吗,因为我被困了两天多,真的很沮丧,这就是我所拥有的: public class MainActivity extends FragmentActivity { // Refresh menu item private MenuItem refreshMen

我有一个应用程序,你需要登录,然后你会去另一个片段。 现在我看到的所有教程都是静态片段,我不想这样,因为我需要刷新片段。现在,因为我没有找到任何关于facebook登录的好的动态片段教程,所以我只想在这里问一下。有人能帮我吗,因为我被困了两天多,真的很沮丧,这就是我所拥有的:

public class MainActivity extends FragmentActivity {


    // Refresh menu item
    private MenuItem refreshMenuItem;

    private boolean isResumed = false;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //Actionbar
        ActionBar actionBar = getActionBar(); 
        //actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(false);
        actionBar.setDisplayShowTitleEnabled(true);
        actionBar.setTitle("Events");
        actionBar.setDisplayUseLogoEnabled(false);

        uiHelper = new UiLifecycleHelper(this, callback);
        uiHelper.onCreate(savedInstanceState);

        setContentView(R.layout.main);
    }


    @Override
    public void onResume() {
        super.onResume();
        uiHelper.onResume();
        isResumed = true;
    }

    @Override
    public void onPause() {
        super.onPause();
        uiHelper.onPause();
        isResumed = false;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        uiHelper.onActivityResult(requestCode, resultCode, data);
    }

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

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        uiHelper.onSaveInstanceState(outState);
    }

    private 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()) {
                // If the session state is open:
                // Show the authenticated fragment
                Fragment newFragment = new EventFragment();
               FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

               // Replace whatever is in the fragment_container view with this fragment,
               // and add the transaction to the back stack
               transaction.add(R.id.fragment_container, newFragment);

               // Commit the transaction
               transaction.commit();
            } else if (state.isClosed()) {
                // If the session state is closed:
                // Show the login fragment
                Fragment newFragment = new LoginFragment();
               FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

               // Replace whatever is in the fragment_container view with this fragment,
               // and add the transaction to the back stack
               transaction.add(R.id.fragment_container, newFragment);

               // Commit the transaction
               transaction.commit();
            }
        }
    }

    @Override
    protected void onResumeFragments() {
        super.onResumeFragments();
        Session session = Session.getActiveSession();

        if (session != null && session.isOpened()) {
            // if the session is already open,
            // try to show the selection fragment
            Fragment newFragment = new EventFragment();
               FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

               // Replace whatever is in the fragment_container view with this fragment,
               // and add the transaction to the back stack
               transaction.add(R.id.fragment_container, newFragment);

               // Commit the transaction
               transaction.commit();
        } else {
            // otherwise present the splash screen
            // and ask the person to login.
               Fragment newFragment = new LoginFragment();
               FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

               // Replace whatever is in the fragment_container view with this fragment,
               // and add the transaction to the back stack
               transaction.add(R.id.fragment_container, newFragment);

               // Commit the transaction
               transaction.commit();
        }
    }

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.main_actions, menu);
        return super.onCreateOptionsMenu(menu);
    }

     /**
     * On selecting action bar icons
     * */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Take appropriate action for each action item click
        switch (item.getItemId()) {
        case R.id.action_search:
            // search action
            return true;
        case R.id.action_refresh:
            refreshMenuItem = item;
            new RefreshAsyncTask().execute((Void[])null);
            return true;
        case R.id.action_info:
            // help info
            return true;
        case R.id.action_help:
            // help action
            return true;
        case R.id.action_check_updates:
            // check for updates action
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

    private class RefreshAsyncTask extends
          AsyncTask<Void, Void, Void> {

       @Override
       protected Void doInBackground(Void... params) {
        // Create new fragment and transaction
           Fragment newFragment = new EventFragment();
           FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

           // Replace whatever is in the fragment_container view with this fragment,
           // and add the transaction to the back stack
           transaction.replace(R.id.fragment_container, newFragment);

           // Commit the transaction
           transaction.commit();
           return null;
       }
       @Override
       protected void onPostExecute(Void result) {
           // remove the progress bar view
           refreshMenuItem.setActionView(null);


       }   
       @Override
       protected void onPreExecute() {
          super.onPreExecute();
          refreshMenuItem.setActionView(R.layout.actionbar_indeterminate_progress);
       }
    }


}
public类MainActivity扩展了FragmentActivity{
//刷新菜单项
私有菜单项刷新菜单项;
私有布尔值isResumed=false;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//操作栏
ActionBar ActionBar=getActionBar();
//actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(“事件”);
actionBar.setDisplayUseLogoEnabled(false);
uiHelper=新UiLifecycleHelper(此为回调);
uiHelper.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
@凌驾
恢复时公开作废(){
super.onResume();
uiHelper.onResume();
isResumed=真;
}
@凌驾
公共无效暂停(){
super.onPause();
uiHelper.onPause();
isResumed=假;
}
@凌驾
ActivityResult上的公共void(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
onActivityResult(请求代码、结果代码、数据);
}
@凌驾
公共空间{
super.ondestory();
uiHelper.ondestory();
}
@凌驾
SaveInstanceState上受保护的无效(束超出状态){
super.onSaveInstanceState(超出状态);
uiHelper.onSaveInstanceState(outState);
}
private void OnSessionState更改(会话、会话状态、异常){
//仅在活动可见时进行更改
如果(已使用){
FragmentManager=getSupportFragmentManager();
//获取后堆栈中的条目数
int backStackSize=manager.getBackStackEntryCount();
//清除后堆栈
对于(int i=0;i<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />