0

I have a bunch of EditTexts in one of my activities. They are visible on the screen and the user can type into them. The user can also to move to another, completely different screen. But, currently, if he/she goes back to the activity with the EditTexts, then his/her inputs are no longer shown on the [first] screen.

What code can I use to keep the user inputs in the EditTexts on the first screen/first Activity?

Thanks in advance.

EDIT (Just updated code)

package com.example.owner.introductoryapplication;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;

import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class UserDataInputActivity extends AppCompatActivity
{
    String[] genderOptions = {"Male", "Female"};

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_user_data_input);
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, genderOptions);
        AutoCompleteTextView acTextView = (AutoCompleteTextView) findViewById(R.id.GenderPromptValue);
        acTextView.setThreshold(1);
        acTextView.setAdapter(adapter);
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState)
    {
        super.onSaveInstanceState(savedInstanceState);
        savedInstanceState.putString("Agestr",(((EditText) findViewById(R.id.AgePromptValue)).getText().toString()));
        savedInstanceState.putString("Speedstr",(((EditText) findViewById(R.id.GaitSpeedPromptValue)).getText().toString()));
        savedInstanceState.putString("Weightstr",(((EditText) findViewById(R.id.WeightPromptValue)).getText().toString()));
        savedInstanceState.putString("Heightstr",(((EditText) findViewById(R.id.HeightPromptValue)).getText().toString()));
        savedInstanceState.putString("Genderstr",(((EditText) findViewById(R.id.HeightPromptValue)).getText().toString()));
        savedInstanceState.putString("UPDRSstr",(((EditText) findViewById(R.id.UPDRSPromptValue)).getText().toString()));
        savedInstanceState.putString("TUAGstr",(((EditText) findViewById(R.id.TUAGPromptValue)).getText().toString()));
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        ((EditText) findViewById(R.id.AgePromptValue)).setText(savedInstanceState.getString("Agestr"), TextView.BufferType.EDITABLE);
        ((EditText) findViewById(R.id.GaitSpeedPromptValue)).setText(savedInstanceState.getString("Speedstr"), TextView.BufferType.EDITABLE);
        ((EditText) findViewById(R.id.WeightPromptValue)).setText(savedInstanceState.getString("Weightstr"), TextView.BufferType.EDITABLE);
        ((EditText) findViewById(R.id.HeightPromptValue)).setText(savedInstanceState.getString("Heightstr"), TextView.BufferType.EDITABLE);
        ((EditText) findViewById(R.id.HeightPromptValue)).setText(savedInstanceState.getString("Genderstr"), TextView.BufferType.EDITABLE);
        ((EditText) findViewById(R.id.UPDRSPromptValue)).setText(savedInstanceState.getString("UPDRSstr"), TextView.BufferType.EDITABLE);
        ((EditText) findViewById(R.id.TUAGPromptValue)).setText(savedInstanceState.getString("TUAGstr"), TextView.BufferType.EDITABLE);
    }

    public void onGenericMenuClick(View v)
    {
        Intent intent = null;

        ArrayList<String> inputStrings = new ArrayList<>();
        inputStrings.add((((EditText) findViewById(R.id.AgePromptValue)).getText().toString()));
        inputStrings.add((((EditText) findViewById(R.id.GaitSpeedPromptValue)).getText().toString()));
        inputStrings.add((((EditText) findViewById(R.id.WeightPromptValue)).getText().toString()));
        inputStrings.add((((EditText) findViewById(R.id.HeightPromptValue)).getText().toString()));
        if(((EditText) findViewById(R.id.GenderPromptValue)).getText().toString().equals("Male"))
            inputStrings.add("1");
        else //(((EditText) findViewById(R.id.GenderPromptValue)).getText().toString() == "Female")
            inputStrings.add("2");
        inputStrings.add((((EditText) findViewById(R.id.UPDRSPromptValue)).getText().toString()));
        inputStrings.add((((EditText) findViewById(R.id.TUAGPromptValue)).getText().toString()));

        // if you click info buttons (even if the EditTexts are blank) show the Dialogs
        if(v.getId() == R.id.gaitInfoButton || v.getId() == R.id.UPDRSInfoButton || v.getId() == R.id.TUAGInfoButton)
        {
            if(v.getId() == R.id.gaitInfoButton)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this);
                builder.setMessage(R.string.GaitInformationContent)
                        .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id) {}
                        });
                // Create the AlertDialog object and return it
                builder.create().show();
            }
            if(v.getId() == R.id.UPDRSInfoButton)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this);
                builder.setMessage(R.string.UPDRSInformationContent)
                        .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener()
                        {
                            public void onClick(DialogInterface dialog, int id) {}
                        });
                // Create the AlertDialog object and return it
                builder.create().show();

            }
            if(v.getId() == R.id.TUAGInfoButton)
            {
                AlertDialog.Builder builder = new AlertDialog.Builder(UserDataInputActivity.this);
                builder.setMessage(R.string.TUAGInformationContent)
                        .setPositiveButton(R.string.acknowledge, new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int id) {}
                        });
                // Create the AlertDialog object and return it
                builder.create().show();
            }
        }//If input fields are blank, refresh the activity
        else if(inputStrings.get(0).matches("") == true
             || inputStrings.get(1).matches("") == true
             || inputStrings.get(2).matches("") == true
             || inputStrings.get(3).matches("") == true
             || inputStrings.get(4).matches("") == true
             || inputStrings.get(5).matches("") == true
             || inputStrings.get(6).matches("") == true)
        {
            Toast.makeText(this, "Please enter all the fields", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(this, UserDataInputActivity.class));
        }//if the input fields are filled and you aren't clicking any info buttons, then navigate to whatever, specified page
        else
        {
            if(v.getId() == R.id.pasttests)
                intent =  new Intent(this, PastDiagnosticResult.class);
            else if (v.getId() == R.id.currenttest)
            {
                intent =  new Intent(this, CurrentDiagnosticResultActivity.class);
                intent.putExtra("INPUTVALUE",inputStrings);
            }
            else if (v.getId() == R.id.myinfo)
                intent =  new Intent(this, UserDataInputActivity.class);
            else if(v.getId() == R.id.VoiceTestingPrompt)
                intent =  new Intent(this, SpeechRecognition.class);

            if (intent != null) {
                startActivity(intent);
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        }
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.owner.introductoryapplication">

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android:permission.RECORD_AUDIO" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:screenOrientation="portrait">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".HomePageActivity" />

        <meta-data
            android:name="preloaded_fonts"
            android:resource="@array/preloaded_fonts" />

        <activity android:name=".UserDataInputActivity" />
        <activity android:name=".CurrentDiagnosticResultActivity" />
        <activity android:name=".PastDiagnosticResult"></activity>
        <activity android:name=".SpeechRecognition"></activity>
    </application>

</manifest>
Shounak Ray
  • 163
  • 1
  • 11
  • Show the code where you start another activity from `UserDataInputActivity`. – Son Truong Apr 03 '19 at 03:16
  • Updated accordingly. It is HUGE, but the processes are essentially logic. Nothing too fancy. I start other activities inside the `onGenericMenuClick` method. – Shounak Ray Apr 03 '19 at 03:21
  • Can you show your original code which not merge with first answer? If the answer do not solve your problem, please do not edit your question. – Son Truong Apr 03 '19 at 03:30
  • And please add your manifest file as well. – Son Truong Apr 03 '19 at 03:34
  • Sorry, the code I updated in the last edit did not "merge with my first answer." It was what I originally had. I have posted my manifest file as well. – Shounak Ray Apr 03 '19 at 04:35

3 Answers3

1

You can use onSavedInstanceState

@Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
    String message = savedInstanceState.getString("message");
    Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}

from How to use onSavedInstanceState example please

Djaf
  • 256
  • 1
  • 6
0

You need to override onSaveInstanceState(Bundle savedInstanceState) and save your inputs like this

@Override
public void onSaveInstanceState(Bundle savedInstanceState) {
   super.onSaveInstanceState(savedInstanceState);
   savedInstanceState.putString("editTextString", editText.getText().toString());

}

Then you override onRestoreInstanceState(Bundle savedInstanceState) to get saved state

@Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
   super.onRestoreInstanceState(savedInstanceState);
   editText.setText(savedInstanceState.getString("editTextString"), TextView.BufferType.EDITABLE);

}
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Amine
  • 2,241
  • 2
  • 19
  • 41
0

Another Way is SharedPreferences, when redirecting another activity store your data in SharedPreferences as blow

SharedPreferences sharedpreferences;
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString("Agestr","3");
editor.putString("Speedstr", "22");

editor.commit();

When you come back on this screen get this data from SharedPreferences and fill your edit text

String Agestr = prefs.getString("Agestr", ""); 
((EditText) findViewById(R.id.AgePromptValue)).setText(Agestr )
Haresh Ramani
  • 390
  • 1
  • 16