Search code examples
javaandroidonclicklistener

How to get string value from Onclick() to onSaveInstanceState() in Android Studio


public class MainActivity extends AppCompatActivity implements View.OnClickListener
{
    Button buttonshow;
    EditText edittextname;
    TextView textviewshow;
    String etText;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        buttonshow = findViewById(R.id.buttonShow);
        edittextname = findViewById(R.id.editTextName);
        textviewshow = findViewById(R.id.textViewShow);

        buttonshow.setOnClickListener(this);
    }

    @Override
    public void onClick(View v)
    {

        etText = edittextname.getText().toString();
        if (v.getId() == R.id.buttonShow)
        {
            textviewshow.setText(etText);
        }
    }

    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState)
    {
        super.onSaveInstanceState(outState);
        outState.putString("textname", etText);
    }

    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
    {
        super.onRestoreInstanceState(savedInstanceState);
        etText = savedInstanceState.getString("textname");
    }
}

I am learning use of onSaveInstanceState() and onRestoreInstanceState(). So I can restore string when my activity turned into the landscape. But I can't get value from Onclick() to other methods. I need the string value of etText to onSaveInstanceState() and onRestoreInstanceState(). Otherwise, these methods don't work. I can't restore this string value when activity turned to landscape.


Solution

  • Do it like this:

    public class MainActivity extends AppCompatActivity
    {
        private final String TEXT_KEY = "TEXT_KEY";
        Button buttonshow;
        EditText edittextname;
        TextView textviewshow;
        String etText;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.d("MyTag", "onCreate");
    
            buttonshow = findViewById(R.id.buttonShow);
            edittextname = findViewById(R.id.editTextName);
            textviewshow = findViewById(R.id.textViewShow);
    
            buttonshow.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    etText = edittextname.getText().toString();
                    textviewshow.setText(etText);
                }
            });
        }
    
        @Override
        protected void onSaveInstanceState(@NonNull Bundle outState)
        {
            super.onSaveInstanceState(outState);
            Log.d("MyTag", "onSaveInstanceState");
            outState.putString(TEXT_KEY, etText);
        }
    
        @Override
        protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
        {
            super.onRestoreInstanceState(savedInstanceState);
            Log.d("MyTag", "onRestoreInstanceState");
            etText = savedInstanceState.getString(TEXT_KEY);
            textviewshow.setText(etText);
        }
    }
    

    When You restore the variable You have to set it to the View.


    You also don't need ettext variable so to make it more clear You can do it like this (I don't implement OnClickListenner and set it in code, I think it is a better way to do it):

    public class MainActivity extends AppCompatActivity
    {
        private final String TEXT_KEY = "TEXT_KEY";
        Button buttonshow;
        EditText edittextname;
        TextView textviewshow;
    
        @Override
        protected void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            Log.d("MyTag", "onCreate");
    
            buttonshow = findViewById(R.id.buttonShow);
            edittextname = findViewById(R.id.editTextName);
            textviewshow = findViewById(R.id.textViewShow);
    
            buttonshow.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View view)
                {
                    textviewshow.setText(edittextname.getText().toString());
                }
            });
        }
    
        @Override
        protected void onSaveInstanceState(@NonNull Bundle outState)
        {
            super.onSaveInstanceState(outState);
            Log.d("MyTag", "onSaveInstanceState");
            outState.putString(TEXT_KEY, textviewshow.getText().toString());
        }
    
        @Override
        protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState)
        {
            super.onRestoreInstanceState(savedInstanceState);
            Log.d("MyTag", "onRestoreInstanceState");
            textviewshow.setText(savedInstanceState.getString(TEXT_KEY));
        }
    }