1

I am trying to create an internal file in an android application. I have generated the code that works fine with java, but in order to create the internal file I believe I must have the context to do so.

Example: File file = new File(Context.getFilesDir(), "somefile.txt");

The problem I am running into is that the file creation and checks if it is made are maintained in a singleton class that I have created. When using the following

Example: File file = new File("somefile.txt");

everything seems to compile and work, but after closing the application it seems the file wasn't created. This leads me to believe that I need the application directory using the 1st example given. The problem is how do I get the applications context within a single class?

blackexile
  • 13
  • 4

3 Answers3

1

The problem is how do I get the applications context within a single class?

From Android Docs:

There is normally no need to subclass Application. In most situation, static singletons can provide the same functionality in a more modular way. If your singleton needs a global context (for example to register broadcast receivers), the function to retrieve it can be given a Context which internally uses Context.getApplicationContext() when first constructing the singleton.

Create your singleton like this:

// ...
private Context mAppContext = null;
private static MySingleton mSingleton = null;
// ...

private MySingleton(Context context) {
    mAppContext = context;
    // ... other initialization
}

public static MySingleton get(Context context) {
    if (mSingleton == null) {
        /*
         * Get the global application context since this is an
         * application-wide singleton
         */
        mSingleton = new MySingleton(
                context.getApplicationContext());
    }
    return mSingleton;
}

The each time you obtain your singleton from any activity, you have access to the global application context.

You can use it for your creation of files within your singleton like:

public void createFile(String filename) {
    File file = new File(mAppContext.getFilesDir(), filename);
}

Or you can use the other ways mentioned here

nem035
  • 34,790
  • 6
  • 87
  • 99
  • What seems to be the problem? :) – nem035 Sep 23 '14 at 18:54
  • I've been doing this the wrong way! :o So we shouldn't extend Application? – Marija Milosevic Sep 23 '14 at 18:55
  • 1
    @LomaRoma well, it is not a simple yes/no answer, it depends on what you like and what better fits your needs and the needs of your system. Here is a question for more detail on this http://stackoverflow.com/questions/3826905/singletons-vs-application-context-in-android – nem035 Sep 23 '14 at 18:58
  • Great question(and answers)! Thanks! – Marija Milosevic Sep 23 '14 at 20:00
  • Thank you for the response. I am currently working on implementing the changes now. – blackexile Sep 23 '14 at 20:17
  • 1
    This method works. Thanks for the help. The reason I am using the singleton class is to access data from multiple fragment activities with ease. To do so I just wanted one object to be created. I called the MySingleton mSingleTon = MySingleton.get(getApplicationContext()); method in the main activity. To my understanding this will initialize and create the singleton object. So in the fragment activities I can just use mSingleTon.getInstance(); ? – blackexile Sep 23 '14 at 20:59
0

Or you could extend Application class thats already a Singleton. It can be rather usefull :)

package com.example.myapp;

import android.app.Application;
import android.content.Context;

public class MyApp extends Application {
    private static Context context;
    private static MyApp my_instance;

    @Override
    public void onCreate() {
        // TODO Auto-generated method stub
        super.onCreate();
        my_instance = this;
        context = this;
    }

    public static synchronized MyApp getInstance() {
        return my_instance;
    }

    public static synchronized Context getContext() {
        return context;
    }
}
Marija Milosevic
  • 516
  • 4
  • 16
  • This seems similar to a method I tried earlier. Unfortunately it didn't work for me. Probably due to lack of understanding on my end. – blackexile Sep 23 '14 at 20:44
0

The mehod:

new File("filename")

does not create a file on disk.

You need to open the file and write to it for the file to be created, or use

File.createNewFile

Guy
  • 12,250
  • 6
  • 53
  • 70
  • True. I have the code to generate the file and write to it. I made a sample java program to make sure the code works. The problem I ran into was getting the application context so that the file can be made in the internal storage for the android application. – blackexile Sep 23 '14 at 20:43