My question is not similar to other questions about how to open YouTube links. My question is how to open a YouTube link, and then when it opens in the app, it should close the YouTube app and again call my MainActivity
which opens the YouTube app. It should however open the YouTube app from scrath and not just show the previous YouTube Activity which is running in the background.
MainAcitivy --> SecondActivity --> Youtube --> ThirdActivity --> Youtube
But I want the YouTube app to load again from scratch. But currently, I am getting the previously opened YouTube app which was in the background.
MainActivity
Intent intent = new Intent(MainActivity.this,ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
SecondActivity
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(link)));
sleep(10000);
Intent intent=new Intent(getApplicationContext(),ThirdActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
ThirdActivity
sleep(5000);
Toast.makeText(getApplicationContext(),"third",Toast.LENGTH_SHORT).show();
Intent intent=new Intent(getApplicationContext(),SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
I want to load it each time again from scratch, but it's showing me the state where it was paused. If you don't understand my question, please be free to comment and I will try to elaborate more. Thanks in advance.
The following example code will open the Youtube link in the Youtube app if this one is available, otherwise it will open it in a browser:
public static void watchYoutubeVideo(String id) {
Intent appIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
Intent webIntent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + id));
try {
startActivity(appIntent);
} catch (ActivityNotFoundException ex) {
startActivity(webIntent);
}
}
EDIT: To answer your second requirement. Each time you call a new Intent
with this code. It will open the application or browser for this video and it wont show the previous video loaded.
Kotlin version to open Youtube video
fun openYoutubeLink(youtubeID: String) {
val intentApp = Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + youtubeID))
val intentBrowser = Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=" + youtubeID))
try {
this.startActivity(intentApp)
} catch (ex: ActivityNotFoundException) {
this.startActivity(intentBrowser)
}
}
Just call
this.openYoutubeLink("Q-dNnMlaGNg")
The below code will open the youtube app in your phone
Intent intent = new Intent(Intent.ACTION_VIEW, "your youtube url here"); startActivity(intent);
if you want to load the url in your activity put a webview and run the url in the webview
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With