Question

I have this code, I want to retain my value of the editbox from the first input after change or start of new activity.

this what happens in this code:

editbox1 = 1 > start new activity > back to recent activity > editbox1 = null

I need this to happened:

editbox1 = 1 > start new activity > back to recent activity > editbox1 = 1

CODE

package org.example.touch;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.*;
import android.widget.EditText;


public class SettingsClass extends Activity {


    private EditText Alpha;
    private EditText Beta;
    private EditText Charlie;
    private EditText Delta;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.settings);

        Alpha = (EditText) findViewById(R.id.editText1);
        Beta = (EditText) findViewById(R.id.editText2);
        Charlie = (EditText) findViewById(R.id.editText3);
        Delta = (EditText) findViewById(R.id.editText4);    

    }

    public void buttonSBHandler (View view){        

        String Aint = Alpha.getText().toString();
        String Bint = Beta.getText().toString();
        String Cint = Charlie.getText().toString();
        String Dint = Delta.getText().toString();

        Intent startNewActivityOpen = new Intent(SettingsClass.this, GameUi.class);

        startNewActivityOpen.putExtra("Aint", Aint);
        startNewActivityOpen.putExtra("Bint", Bint);
        startNewActivityOpen.putExtra("Cint", Cint);
        startNewActivityOpen.putExtra("Dint", Dint);
        startActivityForResult(startNewActivityOpen, 0);
        //startActivity(new Intent(view.getContext(), GameUi.class));
        }
}
Was it helpful?

Solution

1)One thing is that you can go with shared preference store your value in shared preference and on oncreate() method first check whether shared preference is null or not if it is not null than get the value from shared preference.

or

2)Just make those data of edit text as static like-

static String Aint = Alpha.getText().toString();
      static   String Bint = Beta.getText().toString();
        static String Cint = Charlie.getText().toString();
       static String Dint = Delta.getText().toString();

so whenever you will come back to the activity so the previous data of the edit text will be shown there. hope these thing will work for you perfectly. thanks

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top