forum

Home / DeveloperSection / Forums / Inside OnClickListener I cannot access a lot of things - how to approach?

Inside OnClickListener I cannot access a lot of things - how to approach?

Anonymous User 1323 14-Dec-2015
Inside an OnClickListener I cannot access most variables "outside" of the scope, like this:
findViewById(R.id.Button01).setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v)
            {
                Intent mainApps = new Intent(Intent.ACTION_MAIN);
                mainApps.addCategory(Intent.CATEGORY_LAUNCHER);
                List<ActivityInfo> activities = this.getPackageManager().queryIntentActivities(mainApps, 0);
                /*
                Intent intent = new Intent("com.sygic.drive/com.sygic/drive/.SygicDriveActivity");
                startActivity(intent);*/
            }
        });

in this example I need to get the PacketManager, and I cannot get it since I do not have the Context available inside the OnClickListener.

I could make a static reference outside, and use it inside, but is that correct? Seems odd to have to do that all the time?

Updated on 14-Dec-2015
I am a content writter !

Can you answer this question?


Answer

1 Answers

Liked By