I am implementing Oauth (twitter, google) for an Android application and some users have complained because they cannot log in; after analysing the problem I saw that in some devices sometimes the onNewIntent() is not called and onCreate() method is called instead. So it seems that there is something wrong with activities' tasks, instances,..
Here is my code:
AndroidManifest.xml
<activity
android:name="LoginActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data
android:host="callback"
android:scheme="x-oauthflow" />
</intent-filter>
</activity>
<activity
android:name="FirstActivity"
android:label="@string/app_name"
android:noHistory="true"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
FirstActivity.java
btnGoogle = (Button)findViewById(R.id.btnGoogle);
btnGoogle.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (Utils.bInternetConnection(getApplicationContext())) {
Intent intentGoogle = new Intent(getApplicationContext(), LoginActivity.class);
Bundle b = new Bundle();
b.putInt(Constants.SOCIAL_NETWORK, Constants.SOCIAL_NETWORK_GOOGLE);
intentGoogle.putExtras(b);
startActivity(intentGoogle);
} else {
Utils.showDialog(FirstActivity.this, R.string.no_internet_title, R.string.no_internet_msg);
}
}
});
btnTwitter = (Button)findViewById(R.id.btnTwitter);
btnTwitter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (Utils.bInternetConnection(getApplicationContext())) {
Intent intentTwitter = new Intent(getApplicationContext(), LoginActivity.class);
Bundle b = new Bundle();
b.putInt(Constants.SOCIAL_NETWORK, Constants.SOCIAL_NETWORK_TWITTER);
intentTwitter.putExtras(b);
startActivity(intentTwitter);
} else {
Utils.showDialog(FirstActivity.this, R.string.no_internet_title, R.string.no_internet_msg);
}
}
});
LoginActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Async Load
new loadPage(this).execute("");
}
/**
* The callback URL will be intercepted here.
*/
@Override
public void onNewIntent(Intent intent) {
super.onNewIntent(intent);
....
....
}
private class loadPage extends AsyncTask<String, Integer, Void> {
private Context context;
public loadPage(Context context) {
this.context = context;
}
private ProgressDialog pdia;
@Override
protected void onPreExecute() {
super.onPreExecute();
pdia = new ProgressDialog(LoginActivity.this);
pdia.setMessage(getString(R.string.loading));
pdia.setCanceledOnTouchOutside(false);
pdia.show();
}
@Override
protected Void doInBackground(String... arg0) {
try {
Bundle b = getIntent().getExtras();
int socialNetwork = b.getInt(Constants.SOCIAL_NETWORK);
_socialNetwork = socialNetwork;
switch (_socialNetwork) {
case Constants.SOCIAL_NETWORK_GOOGLE:
consumer_key = Constants.CONSUMER_KEY_GOOGLE;
consumer_secret = Constants.CONSUMER_SECRET_GOOGLE;
url_request_token = Constants.URL_REQUEST_TOKEN_GOOGLE
+ "?scope="
+ URLEncoder
.encode(Constants.SCOPE_GOOGLE, "utf-8");
url_access_token = Constants.URL_ACCESS_TOKEN_GOOGLE;
url_authorize_token = Constants.URL_AUTHORIZE_TOKEN_GOOGLE;
break;
case Constants.SOCIAL_NETWORK_TWITTER:
consumer_key = Constants.CONSUMER_KEY_TWITTER;
consumer_secret = Constants.CONSUMER_SECRET_TWITTER;
url_request_token = Constants.URL_REQUEST_TOKEN_TWITTER;
url_access_token = Constants.URL_ACCESS_TOKEN_TWITTER;
url_authorize_token = Constants.URL_AUTHORIZE_TOKEN_TWITTER;
break;
}
// Google & Twitter
consumer = new CommonsHttpOAuthConsumer(consumer_key,
consumer_secret);
provider = new CommonsHttpOAuthProvider(url_request_token,
url_access_token, url_authorize_token);
// Get authUrl
String authUrl = provider.retrieveRequestToken(consumer,
callBack.toString());
// Bring the user to authUrl
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(authUrl))
.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP
| Intent.FLAG_ACTIVITY_NO_HISTORY
| Intent.FLAG_FROM_BACKGROUND);
context.startActivity(intent);
} catch (Exception e) {
Log.e(Constants.TAG, "Oauth error", e);
// Redirect to Login Page
Intent intentFirst = new Intent(getApplicationContext(),
FirstActivity.class);
startActivity(intentFirst);
finish();
}
return null;
}
@Override
protected void onPostExecute(Void unused) {
if (pdia.isShowing()){
pdia.dismiss();
}
}
}
Any ideas?