Why does Edittexts information disappear when I change Victoria?

Asked

Viewed 440 times

2

I have a problem in which information such as "name", "address" and "phone" that I type inside the EditText of Activity main disappear when I go to another Activity seek other types of information and go back to the main one containing the EditText.

I have tried using this setting:

... android:configChanges="orientation|keyboard|keyboardHidden">

Yet the data from Activity major sum.
Why this happens and how can I solve it?

Activity leading:

public class ServicoDeEmailActivity extends ActionBarActivity {

    Session session = null;
    ProgressDialog pdialog = null;
    Context context = null;
    String rec;
    String subject;
    String textMessage;
    EditText editTextValorDoPedido;
    TextView txtValorTotalDoPedido;
    double valorDoPedido;
    int dados;
    String nomeDoProduto;
    String descricaoDoProduto;
    int quantidadeDoProduto;
    double precoUnitarioDoProduto;

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

        this.getWindow().setSoftInputMode(
                WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);

        context = this;

        final EditText nome;
        final EditText rua;
        final EditText numero;
        final EditText complemento;
        final EditText bairro;

        App app = (App) getApplicationContext();
        ItemCompra itemCompra = new ItemCompra();

        Log.i("ServicoDeEmailActivity", "Entrou no metodo onCreate()");

        final EditText telefone1 = (EditText) findViewById(R.id.telefone1);
        telefone1.addTextChangedListener(Mask
                .insert("(##)####-####", telefone1));

        final EditText telefone2 = (EditText) findViewById(R.id.telefone2);
        telefone2.addTextChangedListener(Mask
                .insert("(##)####-####", telefone2));

        final EditText cep = (EditText) findViewById(R.id.cep);

        cep.addTextChangedListener(Mask.insert("#####-###", cep));

        Button botao = (Button) findViewById(R.id.botaoIrParaProdutos);

        botao.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.i("ServicoDeEmailActivity", "Passou pelo metodo onClick()");
                Log.i("ServicoDeEmailActivity", "Chamando Lista de produtos");
                Intent irParaListaDeProdutos = new Intent();
                irParaListaDeProdutos.setClass(ServicoDeEmailActivity.this,
                        ProdutoActivity.class);

                startActivity(irParaListaDeProdutos);
                Log.i("ServicoDeEmailActivity", "Lista de produtos chamada!");
            }
        });

        nome = (EditText) findViewById(R.id.nome);
        rua = (EditText) findViewById(R.id.rua);
        numero = (EditText) findViewById(R.id.numero);
        complemento = (EditText) findViewById(R.id.complemento);
        bairro = (EditText) findViewById(R.id.bairro);

        Intent intentRecuperaDadosDaLista = getIntent();
        Bundle parametroDadosDaLista = intentRecuperaDadosDaLista.getExtras();

        if (parametroDadosDaLista != null) {
            nomeDoProduto = parametroDadosDaLista.getString("nomeDoProduto");
            descricaoDoProduto = parametroDadosDaLista.getString("descricaoDoProduto");
            precoUnitarioDoProduto = parametroDadosDaLista.getDouble("precoUnitarioDoProduto");
            quantidadeDoProduto = parametroDadosDaLista.getInt("quantidadeDoProduto");
        }

        Button btnEnviarPedidoParaEmail = (Button) findViewById(R.id.botaoEnviaPedido);

        btnEnviarPedidoParaEmail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                rec = "DIGITE O EMAIL DE QUEM IRÁ RECEBE-LO";
                subject = "Pedido Solicitado";
                textMessage = "Nome: " + nome.getText()
                        + "<br />" + "Rua: " + rua.getText()
                        + "<br />" + "Número: " + numero.getText()
                        + "<br />" + "Complemento: " + complemento.getText()
                        + "<br />" + "Bairro: " + bairro.getText()
                        + "<br />" + "CEP: " + cep.getText()
                        + "<br />" + "Telefone: " + telefone1.getText()
                        + "<br />" + "Celular: " + telefone2.getText()
                        + "<br />" + "Valor total do Pedido: " + CurrencyUtils.format(BigDecimal.valueOf(valorDoPedido))
                        + "<br />" + "-------------------------------"
                        + "<br />" + "Lista de itens solicitados:"
                        + "<br />" + "Produto: " + nomeDoProduto
                        + "<br />" + "Descrição :" + descricaoDoProduto
                        + "<br />" + "Preço unitário: " + CurrencyUtils.format(BigDecimal.valueOf(precoUnitarioDoProduto))
                        + "<br />" + "Quantidade :" + quantidadeDoProduto;

                Properties props = new Properties();

                props.put("mail.smtp.host", "smtp.gmail.com");
                props.put("mail.smtp.socketFactory.port", "465");
                props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
                props.put("mail.smtp.auth", "true");
                props.put("mail.smtp.port", "465");

                session = Session.getDefaultInstance(props, new Authenticator() {
                    @Override
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication("DIGITE O EMAIL AQUI", "DIGITE A SENHA AQUI");
                    }
                });

                pdialog = ProgressDialog.show(context, "", "Enviando o pedido para o email...", true);

                RetreiveFeedTask task = new RetreiveFeedTask();
                task.execute();

            }

            class RetreiveFeedTask extends AsyncTask<String, Void, String> {

                @Override
                protected String doInBackground(String... params) {
                    try {
                        Message message = new MimeMessage(session);
                        message.setFrom(new InternetAddress("[email protected]"));
                        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(rec));
                        message.setSubject(subject);
                        message.setContent(textMessage, "text/html; charset=utf-8");

                        Transport.send(message);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    return null;
                }

                protected void onPostExecute(String result) {
                    pdialog.dismiss();

                    Toast.makeText(getApplicationContext(), "Pedido enviado com sucesso!", Toast.LENGTH_SHORT).show();
                }
            }

        });

        txtValorTotalDoPedido = (TextView) findViewById(R.id.txt_valor_total);

        Intent intent = getIntent();
        Bundle params = intent.getExtras();
        if (params != null) {
            this.valorDoPedido = params.getDouble("ValorDoProduto");
            txtValorTotalDoPedido.setText(CurrencyUtils.format(BigDecimal.valueOf(valorDoPedido)));
        }

    }
}

And the layout:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

    <LinearLayout
        android:id="@+id/layoutTodo"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textViewProdutos"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="@string/nome_" />

        <EditText
            android:id="@+id/nome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="@string/rua_" />

        <EditText
            android:id="@+id/rua"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="@string/n_mero_" />

        <EditText
            android:id="@+id/numero"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="@string/complemento_" />

        <EditText
            android:id="@+id/complemento"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="@string/bairro_" />

        <EditText
            android:id="@+id/bairro"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="18sp"
            android:textStyle="bold"
            android:text="@string/cep_" />

        <EditText
            android:id="@+id/cep"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >

            <TextView
                android:id="@+id/textView7"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:text="@string/telefone_1_" />

            <EditText
                android:id="@+id/telefone1"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="phone" />

            <TextView
                android:id="@+id/textView8"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:text="@string/telefone_2_" />

            <EditText
                android:id="@+id/telefone2"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:inputType="phone" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/botaoIrParaProdutos"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Lista de Produtos" />

            <TextView
                android:id="@+id/textView9"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="bold"
                android:text="@string/total_r_" />

            <TextView
                android:id="@+id/txt_valor_total"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="18sp"
                android:textStyle="normal"/>

        </LinearLayout>

        <Button
            android:id="@+id/botaoEnviaPedido"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Enviar Pedido" />
    </LinearLayout>
</ScrollView>
  • At some point you change the orientation of Device

  • No ramaral, I haven’t changed the orientation

  • In principle this should only happen if there was a change of orientation of the device or Android destroyed your Activity. I tried to understand his code but he’s very confused.

3 answers

1

For some reason your Activity must be being destroyed and possibly the method onCreate() is being called again to recreate your Activity again. This occurs when the smartphone needs memory, maybe your Apple is "heavy" and the device requires more memory, in which case it needs to displace.

Note the code below, the value android:launchMode="singleTop" maybe it’ll help you.

The line must be inserted in the file Androidmanifest.xml, see an example.

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:launchMode="singleTop"
        android:screenOrientation="portrait">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

Reference: manifest/Activity-element.html

  • This will just "try to get around the problem"! And I don’t know if it will, if in fact, be a memory problem

  • In this case it has not worked and what I have in the other Activity are only 3 items on a list where I use Viewholder that search the values of the products selected times the values. I don’t think it requires that much memory

0

My response is directed to information restoration with:

@Override
protected void onRestoreInstanceState(Bundle savedState) {
   super.onRestoreInstanceState(savedState);
}

.....

In the Edittext

android:saveEnabled="true"

EXAMPLE:

package com.example.statechange;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.util.Log;
import android.widget.EditText;

public class StateChangeActivity extends Activity {
.
.
.
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        Log.i(TAG, "onSaveInstanceState");

        final EditText textBox = 
                (EditText) findViewById(R.id.editText1);
        CharSequence userText = textBox.getText();
        outState.putCharSequence("savedText", userText);

    }
.
.
.

RESTORING:

protected void onRestoreInstanceState(Bundle savedState) {      
        Log.i(TAG, "onRestoreInstanceState");

        final EditText textBox = 
               (EditText) findViewById(R.id.editText1);

        CharSequence userText = 
               savedState.getCharSequence("savedText");

        textBox.setText(userText);
    }

source: http://www.techotopia.com/index.php/Saving_and_Restoring_the_User_Interface_State_of_an_Android_Activity

  • Interesting, although my Activity is very large, I think I need to call the onCreate() method, correct?

0


Guys, I got the problem solved.

I am using startActivityForResult() and at the end of Activity onActivityResult() methods, treating what I need as a return.

Already in the second Activity, I created an Intent passing the values I needed, finally passing setResult(RESULT_OK, Intent) and Finish();

What was happening, is that in this second Activity I was using startActivity() to call the first Activity, only that I lost the values of Edittexts because startActivity() was staging a new Activity.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.