6

I need to be able to be able to call readAsset from outside of the main activity of my application. I have heard people mention needing to pass the Context around, but the language has been very vague. Can someone describe the steps necessary to add the ability to call readAsset to an existing class that is not the main activity? Creating a public function in the main activity and having others call that will not work as the place I need to add readAsset to, is in a separate thread.

corbin
  • 1,446
  • 2
  • 27
  • 40
  • What is your apprehension to passing `Context` to classes that would depend on the functionality? – Quintin Robinson Nov 08 '10 at 17:39
  • I have none, but I haven't found an example of what I need to do. I imagine it is simple, but am only finding vague descriptions. Say I have my main activity and some other class (in this case, in a separate thread). What do need to add in the other class, such that my assets will be available to me there. – corbin Nov 08 '10 at 17:43

3 Answers3

8
public class NonActivity {
    public void doStuff(Context c) {
        //read from assets
        c.getAssets();
        //use assets however
    }
}

Not sure what you're asking, but perhaps something like this? Just add to the existing class, and use the context to retrieve the assets. In your activity call the method like this:

public class MyActivity extends Activity {
  public void OnCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    NonActivity n = new NonActivity();
    n.doStuff(this);
  }
}
danh32
  • 6,234
  • 2
  • 38
  • 39
  • This looks like what I was asking for. I will try this tonight. Thanks! – corbin Nov 08 '10 at 18:07
  • This was it. Thought it would be easy. – corbin Nov 11 '10 at 18:18
  • 1
    be careful with passing around your activity's context. If you are passing the context to a thread that outlives the activity you could have a memory leak. – MikeIsrael Dec 31 '13 at 08:17
  • This actually does not make sense nor answers the original question. The right question should be asked "How to access assets/resources" without any context. Assume a properties file app.properties contains isLogEnabled. How do you access such global property without passing the context around to every class that logs something. – MG Developer Jul 13 '17 at 02:38
  • @MGDeveloper that's a completely different question, though. – danh32 Jul 13 '17 at 16:25
2

To read assets, you need a Context, but you don't have to use an Activity as your Context; you can use the Application object instead.

Android Context without being in an activity? And other activity-less programming?

public class MyApplication extends Application {
    private static MyApplication instance;

    public MyApplication() {
        instance = this;
    }

    public static MyApplication getInstance() {
         return instance;
    }
}

You'll need to add the android:name attribute to the <application> element in AndroidManifest.xml first:

 <application android:name="com.example.MyApplication" ... />

Now you can call MyApplication.getInstance().getAssets() statically from anywhere.

Alternately, you could use Dagger dependency injection to inject an Application directly into your object. (Injecting the Application context is a bit tricky. See Dagger 2 injecting Android Context and this issue filed on the Danger github repo.)

Dan Fabulich
  • 37,506
  • 41
  • 139
  • 175
0

Note all file system accesses should be done off the main thread, so you shouldn't read them during onCreate(). Instead you should use another thread, such as provided by an AysncTask.

Tom O'Neill
  • 61
  • 1
  • 4