0
public  class OuterClass extends Something{

    private int unit = 0;

    private void methodX(int num){
        //Do something here
    }

     public static class InnerClass extends BroadcastReceiver {

        @Override
        public void onReceive(Context context, Intent intent) {     

        // Need to call methodX(100) here       
        }
     }      
}

I am crating an application and it works fine. but when I am using

static OuterClass instance;

in OuterClass and access its variable through

instance.methodX(100)

from inner class it is leading to memory leak. If I remove static keyword from inner class Broadcast receiver not fired.

this is the part of my manifest file.

<receiver
    android:name=".OuterClass$InnerClass"
    android:enabled="true"
    android:exported="true">
    <intent-filter >
        <action android:name="com.xyz.abc.RESULT"/>        
        <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>
</receiver>

This is working as I expected but it has some memory leak. I need to access outer class method from static inner class without a memory leak.for that I should avoid using static instance of outer class.

I am really grateful if anyone can help me to find a way to access outerClass methodX from inner class.

D. Joe
  • 19
  • 1
  • it depends on what `Something` is ... if it is `Application` class then there should be no problem with outer class instance ... if it is `Activity` or `Fragment` such code doesn't make sens as Activity/Fragment may not exists at the time of creating InnerClass – Selvin Dec 11 '17 at 10:46
  • ultimately it is extending Activity – D. Joe Dec 11 '17 at 10:56
  • So use 2nd BroadcastReceiver as inner non static ... register in onCreate and unregister it in onStop (so there will be no leak) ... and send broadcast to it from onReceive of your 1st BroadcastReceiver ... eventually you may use event bus for the same purpose – Selvin Dec 11 '17 at 10:57

2 Answers2

1

Declaring a static inner class is similar to creating a new file, in the terms that you need an instance for the outter class for it to work.

Non-static inner classes work just like non-static fields: they're instance specific. Meaning you need an instance of the outter object to initialize a new inner object.

When it is static, you don't need an object of the outter class to work.

For non-static inner classes, you can do this:

outter.new Inner()

and get access to the class that it's nested in.

But since your class is static, you have to pass an instance like normal. I.e.

new Outter.Inner(outterInstance)

However: Since the class extends BroadcastReceiver, it initializes into an empty constructor. Meaning the constructor with values to pass will never be used, since BroadcastReceiver is a system-initialized and handled system. Create a new instance in the empty constructor instead, or move all the variables into what is currently the inner class

Since BroadcastReceiver requires an empty constructor, doing this is not a possibility:

public Inner(Outter instance)

It will not get initialized and you'll probably get exceptions from it too. You can, however, do this:

public Inner(){
    outter = new Outter();
}

Or alternatively, move all the outter class fields and methods into the BroadcastReceiver.

You should read this SO post on the topic

Zoe
  • 27,060
  • 21
  • 118
  • 148
1

You can just mark that method methodX() as static. After this you will be able to access that method in you static inner class.

AskNilesh
  • 67,701
  • 16
  • 123
  • 163
Abdul Waheed
  • 4,540
  • 6
  • 35
  • 58
  • Not a good idea, there may be instance-specific vars OP is trying to access – Zoe Dec 11 '17 at 10:38
  • Hmm you are right but in current case method is not accessing any instance variables. – Abdul Waheed Dec 11 '17 at 10:39
  • `//Do something here` is essentially a massive wildcard in the code. It could be the case that there is something in that code that accesses instance vars – Zoe Dec 11 '17 at 10:41