Skip to content

Instantly share code, notes, and snippets.

@Yincan
Created June 13, 2011 07:17
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save Yincan/1022415 to your computer and use it in GitHub Desktop.
Save Yincan/1022415 to your computer and use it in GitHub Desktop.
Android Activity LaunchMode attribute
<activity android:launchMode = ["standard" | "singleTop" | "singleTask" | "singleInstance"] ../>
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
Uri uri = intent.getData();
if (uri != null && uri.toString().startsWith(CALLBACK_URL)) {
String verifier = uri.getQueryParameter("oauth_verifier");
try {
//load other persisted data goes here
....
//get the request-token from prefs and request the access token
TwitterFactory twitter = new TwitterFactory().getInstance();
requestToken = prefsManager.getRequestToken();
prefsManager.clearTwitterRequestToken();
twitter4j.auth.AccessToken accessToken = twitter.getOAuthAccessToken(requestToken, verifier);
// save the access token
prefsManager.saveTwitterOauth(accessToken.getToken(), accessToken.getTokenSecret(), accessToken
.getScreenName());
//other logics to do the post to twitter
....
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
}
}
}
<activity android:name=".MainTabActivity" android:launchMode="singleTask" android:alwaysRetainTaskState="true" android:windowSoftInputMode="adjustPan">
</activity>
<activity android:name=".NewsDetailActivity" android:launchMode="singleTop">
<intent-filter>
<action android:name="android.intent.action.VIEW"></action>
<category android:name="android.intent.category.DEFAULT"></category>
<category android:name="android.intent.category.BROWSABLE"></category>
<data android:scheme="oauth" android:host="twitt"></data>
</intent-filter>
</activity>
private void twitterOAuth() {
try {
System.setProperty("twitter4j.http.useSSL", "false");
System.setProperty("twitter4j.oauth.consumerKey", getString(R.string.twitter_oauth_consumer_key));
System.setProperty("twitter4j.oauth.consumerSecret", getString(R.string.twitter_oauth_consumer_secret));
System.setProperty("twitter4j.oauth.requestTokenURL", "http://api.twitter.com/oauth/request_token");
System.setProperty("twitter4j.oauth.accessTokenURL", "http://api.twitter.com/oauth/access_token");
System.setProperty("twitter4j.oauth.authorizationURL", "http://api.twitter.com/oauth/authorize");
// get the instance
Twitter twitter = new TwitterFactory().getInstance();
twitter4j.auth.RequestToken requestToken = twitter.getOAuthRequestToken(CALLBACK_URL);
//save the request token and secret
prefsManager.saveTwitterRequestToken(requestToken.getToken(), requestToken.getTokenSecret());
//save other data
persistNewsDatas();
//start the browser activity
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(requestToken.getAuthenticationURL()));
startActivity(intent);
} catch (Exception e) {
Log.e(TAG, e.getMessage(), e);
Toast.makeText(NewsDetailActivity.this, "Fail to connect to twitter now, please try later.",
Toast.LENGTH_SHORT).show();
}
}
@umbalaconmeogia
Copy link

Is this example correct? It always creates new instance of the NewsDetailActivity when returning back from Twitter web page.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment