srt
srt

Reputation: 245

Retrieve Data from Internal Storage DEFINED in a class ANDROID

I have defined two subclasses LoadData() and SaveData() in a Class to load and save data in the Internal Storage as:

public class LoadSaveInternalActivity extends Activity {
public String LoadData(String filename)
{   String read = "";
    try {
        FileInputStream fis = getBaseContext().openFileInput(filename);
        if (fis != null) {
            BufferedReader br = new BufferedReader(new InputStreamReader(fis));
            read = br.readLine();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return read;
}

public void SaveData(String data, String filename)
{
    try {
        FileOutputStream outputStream = getBaseContext().openFileOutput(filename, Context.MODE_PRIVATE);
        String text = data;
        outputStream.write(text.getBytes());
        outputStream.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
}}

And I am trying to call these methods of the class from another Activity like:

public class Fragment1 extends Fragment {

EditText edittxt;
private final static String file_name = "myfile";
private LoadSaveInternalActivity InternalActivity;@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.frag1, container,false);

    edittxt = (EditText) rootView.findViewById(R.id.editTxt);

    InternalActivity = new LoadSaveInternalActivity();
    String readData = InternalActivity.LoadData(file_name);
    edittxt.setText(readData);

    return rootView;
}
@Override
public void onStop() {
    super.onStop();
    InternalActivity = new LoadSaveInternalActivity();
    InternalActivity.SaveData(edittxt.getText().toString(),file_name);

}

But it doesn't work out. Does anybody can help me in this case?

Upvotes: 0

Views: 45

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006914

I have defined two subclasses LoadData() and SaveData() in a Class

Those are methods. They are not subclasses.

public class LoadSaveInternalActivity extends Activity

Do not create random subclasses of Activity.

InternalActivity = new LoadSaveInternalActivity();

Do not create instances of an Activity yourself via the constructor.

The simplest set of fixes would be:

Step #1: Move your load and save methods into your fragment

Step #2: Change the getBaseContext() calls to getActivity() in those methods

Step #3: Change onCreateView() and onStop() to call the load and save methods in the fragment

Step #4: Delete LoadSaveInternalActivity and all references to it

That code should at least compile and run. It is still fairly poor code (e.g., you are doing disk I/O on the main application thread).

But it doesn't work out

In the future, when you post questions on Stack Overflow, explain in detail what your symptoms are. Statements like "it doesn't work out", without any details, are useless.

Upvotes: 1

Related Questions