Why does the EditTexts information come up when I change my activity?

2

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

I've tried using this setting:

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

Even though the data of the Activity master is. Why does this occur and how can I resolve it?

Activity principal:

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>
    
asked by anonymous 14.02.2015 / 15:26

3 answers

0

Well, I've been able to solve the problem.

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

In the second activity, I created an intent by 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, but I lost the values of EditTexts because startActivity () was stalling a new Activity.

    
14.02.2015 / 21:49
1

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

Look at the code below, the value android: launchMode="singleTop" might help you.

The line should be inserted into the AndroidManifest.xml file , 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

    
14.02.2015 / 16:20
0

My response is directed to the information restore with:

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

.....

No 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: link

    
14.02.2015 / 16:46