Facebook Android API and Twitter Webview Login Integration

I have an Android app that will require the user to log in using either facebook or twitter. I managed to realize both of them and was partly successful. Facebook login was implemented using this facebook tutorial.

As for logging in to Twitter, I just used a button that launches TwitterLoginActivity, which logs into the webview, and if it successfully launches the TwitterHomeActivity function, which is implemented exactly like facebook logic in terms of fragmentation.

So, here is a breakdown of the entire Impedmentation registry

FacebookHomeActivity has 3 fragments

  • FacebookSplashFragment (the ability to log in using fb or twitter)
  • FacebookSelectionFragment (shows the application menu)
  • FacebookUserSettingsFragmen (logout user).

TwitterHomeActivity has 2 snippets

  • TwitterSelectionFragment (shows the application menu)
  • TwitterUserSettingsFragment (logout)

TwitterLoginActivity (contains a web view for authentication through my server, which will redirect to a Twitter page to log in)

The problem, if you haven’t seen it yet, is that if I log out of my Twitter account, I need to run the FacebookHomeActivity function to view my facebook or twitter login options.

Doing this is very hacky and just plain bad. So I thought that if I made all the fragments, this could solve my problem.

LoginActivity (login host) contains:

  • LoginSelectionFragment (select to enter facebook or twitter)
  • TwitterLoginFragment (User Login via Web View)
  • FacebookSelectionFragment (user and show menu authorization)
  • TwitterSelectionFragment (show menu)
  • FacebookUserSettingFragment (logout fb user)
  • TwitterUserSettingFragment (logout twitter user)

Using this so that it displays the correct fragments in different input 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); } } 

Creating login logic exists since fragments seem to make sense to me, but that doesn't behave the way I thought it worked. Instead, it basically runs everything that onStart () has in each fragment, so imagine a lot of progress that comes up and nothing happens. The column was also messed up, causing a fragment that was never shown for display. Either I'm implementing this incorrectly, or this is a completely wrong way to approach this problem.

My question is: what would be the best way to implement both Facebook and Twitter login if this is not the best way to solve the problem.

If the above makes sense and has nothing wrong with that, what do you think I'm doing wrong?

+4
source share
1 answer

About 5 days after testing new methods, such as isolating the Facebook login authentication to its own activity using its own set of fragments that also could not behave properly, I read on the Fragmentation Documentation , which gave me a better idea of ​​how fragments really work and are designed to work. For those who are faced with a similar problem, like me, I strongly recommend that you first read the documentation.

The FragmentManager, or rather, the SupportFragmentManager, which I use with the beginTransaction () method, produces a replace () method that allows you to replace the fragment in the current view with the one you want to display. Here 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(); } } 

The addToBackStack parameter is intended for you to decide whether the user can return to this fragment after displaying a new one.

Therefore, instead of using showFragment (), which is shown in the Android Android API examples, I replaced all showFragment () methods with replaceFragment ().

The difference between 2 is that when using the showFragment () method, when you preload all the fragments in your host activity (in my case LoginActivity.java), what happens is ALL the ALL fragment onCreate () is called onStart () function and onResume (). This behavior was bad, as some of my snippets started executing code in the onStart () methods, forcing all progressDialogs to show when they shouldn't.

Using the replaceFragment () method shown above, I can create an instance of a fragment only when I need to show it, which allows me to use lifecycle methods (onCreate (), onStart (), onResume (), etc.) usually.

After a successful login, I just show the user my menu and clear the back stack in the following way:

 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(); } } 

The key areas for clearing the back stack will be after the user has successfully logged in and when the user has successfully logged out.

With this, I can better manage fragments, as they behave like actions in terms of its life cycle.

Hope this helps someone out there.

+3
source

Source: https://habr.com/ru/post/1502850/


All Articles