Facebook Android API and twitter WebView login integration

I have an Android application that will require users to log in using Facebook or twitter. I managed to achieve both and partially succeeded. Facebook login is implemented using Facebook's this tutorial

As for twitter login, I just used a button to start the twitterloginactivity that performs WebView login. If I successfully start twitterhomeactivity, it implements a fragment similar to Facebook login

So this is a breakdown of the entire login implementation

Facebook homeactivity has 3 fragments

>Facebook splashfragment (option to log in with FB or twitter) > Facebook selectionfragment (show application menu) > Facebook usersettingsfragment

Twitterhomeactivity has 2 fragments

>Twitterselectionfragment > twitterusersettingsfragment

Twitterloginactivity (including a WebView, which is authenticated through my server and redirected to Twitter page for login)

Problem, if you haven't seen it yet, if I log out of my twitter account, I have to start Facebook homeactivity just to see the option to log in to Facebook or twitter

This is very hacking and bad. So I think if I fragment it all, it may solve my problem

Loginactivity contains:

>Loginselectionfragment (choose to log in using Facebook or twitter) > twitterloginfragment (log in user via WebView) > Facebook selectionfragment (authorized user and program menu) > twitterselectionfragment (program menu) > Facebook usersettingfragment (log out FB user) > twitterusersettingfragment (log out of twitter user)

Use it to display the correct fragments in different login states:

        @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
        getSupportActionBar().show();
        showFragment(FACEBOOKSELECTION, false);
    }

    else if (Globals.loggedIn() && Globals.isTwitterUser()) {
        // otherwise present the splash screen
        // and ask the person to login.
        getSupportActionBar().show();
        showFragment(TWITTERSELECTION, false);

    } 
    else {
        // otherwise present the splash screen
        // and ask the person to login.
        Globals.logout();
        getSupportActionBar().hide();
        showFragment(LOGINSELECTION, false);
    }

}

Making all login logic exist as fragments seems to make sense to me, but it doesn't work as I thought. On the contrary, it basically runs everything in onstart() in each fragment, so imagine that many progressdialogs will pop up and nothing else will happen. The backplane also screwed up, Either I implemented the error, or it was a completely wrong way to solve the problem

My question is: if this is not a good way to solve the problem, what is a better way to implement Facebook login and twitter login

If the above is meaningful and there is no problem, what do you think I did wrong?

resolvent:

After about 5 days of trying new methods, such as isolating Facebook login authentication to your own activity and using your own set of fragments, which do not run as they should. I read fragments documentation, which makes me better understand the actual work of fragments and plan to work. For those who encounter similar problems, I strongly recommend you to read the documentation first

The fragmentmanager or the supportfragmentmanager I used and the begintransaction () method expose a method called replace (), which allows you to replace the fragment in the current view with the fragment you want to display. This is how I use it:

  public void replaceFragment(Fragment f, Boolean addToBackStack) {
        if (addToBackStack) {
            getSupportFragmentManager().beginTransaction()
                    .replace(android.R.id.content, f).addToBackStack(null).commit();
        } else {
            getSupportFragmentManager().beginTransaction()
                    .replace(android.R.id.content, f).commit();
        }
    }

Therefore, instead of using showfragment () shown in the Facebook Android API example, I replaced all showfragment () methods with replacefragment ()

The difference between 2 is that when you use the showfragment() method, what happens when you pre load all fragments in your host activity (loginactivity. Java in my case), and the oncreate(), onstart() and onresume() functions of all fragments are called. This behavior is very bad because some of my fragments start to execute code in the onstart() method, Causes all progressdialogs to be displayed when they should not be displayed

By using the replacefragment () method shown above, I can instantiate fragments only when they need to be displayed, allowing me to use life cycle methods normally (oncreate(), onstart(), onresume(), etc.)

After the user successfully logs in, I just need to display my menu to the user and clear the background stack using the following methods:

public void clearBackStack() {
    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();
    }
}

With this, I can better control fragments because they behave like activities in the life cycle

I hope it will help the people there

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>