0

Simple one, I need to access a global variable from inside a onClickListener, but the instance has no knowledge of the "this" pointer. Is there an easy way to do this:

    activityButton = (Button) findViewById(R.id.actbtn);
    activityButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (fragmentCommunicator != null) {
                // cannot really do this. I need to pass the "this pointer"
                int counter = ((MyApplication) this.getApplication()).getGlobalCounter();                    counter++;
                fragmentCommunicator.passDataToFragment("Hi from activity!, global counter = " + counter);
            }

        }

I tried as a custom OnClickListener

How to pass parameters to OnClickListener?

but is there a compact syntax? In cpp++ the would be the equivalent of lambda capture [=]

thx!

Community
  • 1
  • 1
gmmo
  • 2,577
  • 3
  • 30
  • 56

0 Answers0