0

The function is working well in MainActivity.kt, but it has an error in a fragment.

  • 1) Please kindly help and explain it.
  • 2) I still not understand about the context issue, please kindly help to give any easy understanding link/information for me to read.

    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)
    }
    

    }

Error Image: enter image description here

GPH
  • 1,111
  • 6
  • 29
  • 49
  • 1
    This could be of help https://blog.mindorks.com/understanding-context-in-android-application-330913e32514 or this https://stackoverflow.com/questions/3572463/what-is-context-on-android – hushed_voice May 21 '18 at 08:04
  • 1
    In MainActivity "this" will give you Activity because MainActivity is an Activity(extends activity obviously) , but in the fragment "this" will refer to the fragment. Fragment does not have the method startactivity. hence the error – hushed_voice May 21 '18 at 08:08
  • 1) Leave that function in the Activity that calls the Fragment. 2) Create an Interface in your Fragment. 3) Handle the callback in the Activity: call openYoutubeLink there! – CEO tech4lifeapps May 21 '18 at 09:12

1 Answers1

3

Because the fragment is not a Activity. You need to request the activity in wich the fragment is nested. Waht you need to do :

requireActivity().startActivity(intentApp)

EDIT: Well since you want to use it by addapter this is the correct way(Think of better names):

class MyFragment implements AdapterListener {
        Adapter adapter;

        @Override
        void onResume() {
            super.onResume();
            adapter = new Adapter(this);
        }

        @Override
        void onPause(){
            super.onPause();
            adapter = null;
        }

        @Override
        void openActivity(){
            ...
            requireContext().startActivity()
        }
    }

    class Adapter {

        ...

        interface AdapterListener{
            void openActivity();
        }
    }
Ivan
  • 557
  • 1
  • 3
  • 14
  • thanks, but i would like run this function in an adapter class, the fragment will use this adapter, what should i write? the requireActivity() shows error. – GPH May 21 '18 at 09:01
  • 1
    What you want to do is not good architecture. Noramly you want to keep the buisness logic separated from the UI. Opening other activities is a job for the UI elements i.e. Fragment/Activity. You should have a callback from the Adapter something like openMyActivity() wich is implemented by the Fragment and this way the fragment should open it. But if you insist on doing it your way you should pass the requireActivity() to the Adapter as WeakReference<>. – Ivan May 21 '18 at 10:31