1

I have a main activity that has a button. Pressing the button opens a child activity. Inside the child activity is a button. Pressing that button opens an implicit intent (YouTube video). Here's my code in the child activity:

startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("some_youtube_link")));

The video starts fine but when I press back it just keeps going back within the YouTube app which is undesirable. So I added this line:

finish();

...on the next line just after starting the implicit intent. Now when I press back on the video, it goes all the way back to the main activity. But I want it to go back to the activity that started it (the child activity). How can I achieve this?

Edit: My was flagged as a duplicate of this question. However my question is different. Whereas in the referenced question, the OP wants the YouTube app to close and return to his app after the video is finished and not have to press the back button, I want the back button to take me back to the activity that started the implicit intent and not the parent activity.

Update: I removed the finish(); line and it works perfectly fine on an actual device and the behavior is exactly what I'm looking for. However on the emulator, if I press back quickly enough after the YouTube app has loaded then I will go back to the child activity as desired. But if I wait a few second and press back, it will just keep going to the beginning of the YouTube video. In any case, I don't have a problem anymore since all I care about is the device not the emulator, but I still don't understand why the emulator behaves that way.

Update: OK, problem still persists. When I click the play button, a dialogue opens up asking me to choose my application (YouTube, web browser, etc). If I select YouTube and then choose the "Just Once" option instead of the "Always" option, then when YouTube opens and I press the back button, it will go back to the child activity (as desired) and display the "choose application" again. I have to press back a second time to get rid of this popup. This is annoying but I can deal with it. The problem is when I select the "Always" option. Now when I press back from YouTube, it keeps reopening the same YouTube video and there's no way for me to go back to my app. It seems that there's an intermediary activity (between my app and YouTube) that gets executes and this intermediary activity is the "choose application" menu. But since I have chosen "Always" as the option, as soon as it goes back to the "choose" menu it automatically chooses YouTube and opens up YouTube again. How can I fix this?

Shadab K
  • 1,677
  • 1
  • 16
  • 25
Arash Fotouhi
  • 1,933
  • 2
  • 22
  • 43
  • Possible duplicate of [Return back from youtube to my android app](https://stackoverflow.com/questions/10494899/return-back-from-youtube-to-my-android-app) – dustblue Dec 13 '17 at 14:20
  • @dustblue Please see my edited question where I explain why my question is different than the one you mentioned. – Arash Fotouhi Dec 13 '17 at 14:50

0 Answers0