-1

Maybe this answer exists but I couldn't find it here nor google.

I have a static singleton class named Handler (non activity class). I added a library to my project and I would like to use the Handler in MainActivity of the library.

-- app
    --java
        --com.mytest
            --Handler
-- library
    --java
        --com.mylib
            --MainActivity

I've tried to import com.mytest.Handler in MainActivity but it didn't work. Maybe I should add some dependency to the library? I'm really lost.

EDITED:

MainActivity code:

public class MainActivity extends AppCompatActivity {
    private Button mButtonMoreApps = null;
    private ArrayList<String> mListUrl = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(com.mylib.R.layout.main_activity);

    }
}

mylib gradle:

apply plugin: 'com.android.library'

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.2"
    defaultConfig {
        minSdkVersion 15
        targetSdkVersion 25
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner     "android.support.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        debug {
            signingConfig signingConfigs.config
            debuggable true
        }
    }
}

dependencies {
    compile fileTree(include: ['*.jar'], dir: 'libs')
    androidTestCompile('com.android.support.test.espresso:espresso-    core:2.2.2', {
        exclude group: 'com.android.support', module: 'support-annotations'
    })
    compile "com.android.support:cardview-v7:25.3.1"
    compile 'com.android.support:design:25.3.1'
    compile 'com.android.support:customtabs:25.3.1'
    compile 'com.android.support:appcompat-v7:25.3.1'
    compile 'com.android.support.constraint:constraint-layout:1.0.2'
    compile 'com.google.firebase:firebase-core:10.2.1'
    compile 'com.google.firebase:firebase-database:10.2.1'
    compile 'com.google.firebase:firebase-ads:10.2.1'
    compile 'com.google.firebase:firebase-crash:10.2.1'
    compile 'com.facebook.android:audience-network-sdk:4.20.0'
    compile ('com.facebook.android:facebook-android-sdk:4.20.0') // app     invites
    {
        exclude group: 'com.android.support'
    }
    testCompile 'junit:junit:4.12'
}
Maor Cohen
  • 936
  • 2
  • 18
  • 33

1 Answers1

0

You shouldn't call application method from a library by importing to library.You can use listeners instead.

For an example : https://stackoverflow.com/a/31626226/2176401

Library class corresponds to Observable class there and your application class should implement Observer. After that, you can use notifyObservers method to call the code you write in Handler class

cancit
  • 172
  • 5