Search code examples
androidandroid-activityoauthandroid-sharing

Android OAuth implementation makes receiving share activity stop


I have a problem with a activity where onStop breaks my OAuth authentication process.

First a small explanation about the context of my app:

I have this activity (lets call it the receiveSharedDataActivity) which handles incoming data which is being send from other apps. This activity is made using the following example from the Android Developer site: http://developer.android.com/training/sharing/receive.html.

My app requires the user to be logged in using a E-mail and a password or using the Facebook or Twitter OAuth login. It is possible that the user has never opened the app and thus is not logged in yet. (The possibility is small, but it can still occur.)

The problem (challenge):

So when the user decides to share some data to my app but is not logged in yet, I show him the 3 login options (E-mail, Facebook, and Twitter) in the receiveSharedDataActivity using fragments. This way he can first authenticate himself and after that the data can be handled. When the user chooses to login with Facebook the Twitter OAuth login option he is redirected to a new activity from Facebook or Twitter where he needs to login and give my app permission to access his user data. All goes well up to this point. Because when the user is redirected to this new activity, the receiveSharedDataActivity is automatically being stopped using onStop because it is no longer visible to the user. When the user then logs in at the Facebook or Twitter activity and is being send back to the receiveSharedDataActivity, there is no receiveSharedDataActivity anymore because it has stopped. So the user ends up at another app where the user decided to share data from.

The question:

How can I prevent the receiveSharedDataActivity from being stopped when the user is login in at the Facebook or Twitter activity?

I am looking forward to your solutions because I am pretty stuck with this problem.


Solution

  • Good news! I was able to solve this issue. All this time I thought that it had to do with passing the correct Intent flags when starting the receiveSharedDataActivity. Silly me...

    The problem was that my receiveSharedDataActivity had the noHistory:true parameter set in the AndroidManifest.xml. I needed this because obviously I did not want this activity to be added to the backstack. But the noHistory parameter made my activity destroy itself directly after the activity was no longer visible to the user. Which happend when the OAuth login activity started. So of course there was no activity to return to after the OAuth login activity was completed because the receiveSharedDataActivity was simply destroyed.

    When I removed the noHistory parameter, the activity did not destory itself anymore and nicely called the onRestart method where I could now handle the rest of the Oauth process. And I was able to prevent the activity from being added to the backstack just by calling finish() right after starting a new activity. This way the activity destroys itself and does not get added to the backstack.