electricnerd
electricnerd

Reputation: 37

Android not saving text?

I've been trying to create an app that can save text that you have input in a text box after you click a button to save it, but whenever I try to save text in the app, it cannot do so. For example, I type in "Joe smith 123" in the text box, click the save button, then exit out of the app. A complete exit, even going to the tabs and deleting it from there. But when I reopen the app, the text that I input isn't there. Here is the code for the xml and the class files.

XML:

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.rad.passwordstorage3.Storage">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="SAVE PASSWORDS"
        android:id="@+id/save"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:onClick="saveClicked"/>
    <EditText

        android:id="@+id/textbox"

        android:singleLine="false"

        android:gravity="top"

        android:lines="10"

        android:layout_width="fill_parent"

        android:layout_height="wrap_content"
        android:inputType="none" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Enter your passwords in the textbox!"
        android:id="@+id/textView"
        android:layout_marginTop="41dp"
        android:layout_below="@+id/save"
        android:layout_centerHorizontal="true" />
       </RelativeLayout>

Class:

package com.example.rad.passwordstorage3;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;

public class Storage extends AppCompatActivity {





    /** Called when the activity is first created. */

    public void readFileInEditor()

    {

        try {

            InputStream in = openFileInput(STORETEXT);

            if (in != null) {

                InputStreamReader tmp=new InputStreamReader(in);

                BufferedReader reader=new BufferedReader(tmp);

                String str;

                StringBuilder buf=new StringBuilder();

                while ((str = reader.readLine()) != null) {

                    buf.append(str+"n");

                }

                in.close();

                txtEditor.setText(buf.toString());

            }

        }

        catch (java.io.FileNotFoundException e) {

// that's OK, we probably haven't created it yet

        }

        catch (Throwable t) {

            Toast

                    .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)

                    .show();

        }

    }
    private final static String STORETEXT="storetext.txt";

    private EditText txtEditor;

    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_storage);

        txtEditor=(EditText)findViewById(R.id.textbox);

    }
    public void saveClicked(View v) {

        try {

            OutputStreamWriter out=

                    new OutputStreamWriter(openFileOutput(STORETEXT, 0));

            out.write(txtEditor.getText().toString());

            out.close();

            Toast

                    .makeText(this, "Your passwords have been saved!", Toast.LENGTH_LONG)

                    .show();

        }

        catch (Throwable t) {

            Toast

                    .makeText(this, "Exception: "+t.toString(), Toast.LENGTH_LONG)

                    .show();

        }

    }
}

Is there any problem with my save methods, or something else? Thanks.

Upvotes: 0

Views: 94

Answers (2)

Myo Ko
Myo Ko

Reputation: 108

Here it is, Just add this below line in onCreate() Method:

readFileInEditor();

That is it. You are wrapping your codes in a method and maybe you just forgot to call it in onCreate() Method:

And your this commented line in your code is NOT true:

/** Called when the activity is first created. */

Because the method that is first called when app starts is not the code (or code of blocks) you put there at the first place, but the onCreate() Method you override with the word "@Override". Enjoy!

Upvotes: 1

kikettas
kikettas

Reputation: 1742

When you finish and close your app, the state of it disappear in that moment. If you want to recover that text, you will need to load again that '.txt' within onCreate method.

Nevertheless, if the text it not too long I recommend you to use SharedPreferences instead of OSW, because It quite easier to implement and It uses less system resources.

Here you have more information: Shared Preferences.

Upvotes: 3

Related Questions