Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open page in Twitter app from other app - Android

I was looking for some way to launch Twitter app and open a specified page from my application, without webview. I found the solution for Facebook here: Opening facebook app on specified profile page

I need something similar.

EDIT I just found a solution:

try {     Intent intent = new Intent(Intent.ACTION_VIEW,     Uri.parse("twitter://user?screen_name=[user_name]"));     startActivity(intent); } catch (Exception e) {     startActivity(new Intent(Intent.ACTION_VIEW,     Uri.parse("https://twitter.com/#!/[user_name]")));  } 
like image 878
jbc25 Avatar asked Jun 19 '12 16:06

jbc25


People also ask

Can an Android app open another app?

To take the user from one activity to another, your app must use an Intent to define your app's "intent" to do something. When you pass an Intent to the system with a method such as startActivity() , the system uses the Intent to identify and start the appropriate app component.

How do I open a website without opening the app?

Chrome Still Opening Apps Firstly, copy the link and open it in the incognito mode of Chrome. That will open the website instead of the app. Alternatively, enable the Request desktop site feature of Chrome. For that, tap on the three-dot icon in Chrome and enable Request desktop site.


Video Answer


2 Answers

Based on fg.radigales answer, this is what I used to launch the app if possible, but fall back to the browser otherwise:

Intent intent = null; try {     // get the Twitter app if possible     this.getPackageManager().getPackageInfo("com.twitter.android", 0);     intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=USERID"));     intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); } catch (Exception e) {     // no Twitter app, revert to browser     intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/PROFILENAME")); } this.startActivity(intent); 

UPDATE

Added intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); to fix an issue where twitter was opening inside my app instead of as a new activity.

like image 72
Harry Avatar answered Sep 20 '22 17:09

Harry


This worked for me: twitter://user?user_id=id_num

like image 21
fg.radigales Avatar answered Sep 21 '22 17:09

fg.radigales