How to pass a certain content contained in a direct first Activity to a third Activity?

Asked

Viewed 115 times

0

People would like to know how to pass the contents of a plaintext that is in Mainactivity directly to Main3activity, without having to import to Main2activity for later to Main3activity.

User passes through Main2activity before going to Main3acitvity.

In the Mainactivity has a plaintext (etPlanetaVive) where the user informs which planet he lives on.

inserir a descrição da imagem aqui

Mainactivity.java

ackage genesysgeneration.third;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private EditText etPlantaVive;
    private Button btnNext_01;

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

        etPlantaVive=(EditText)findViewById(R.id.etPlanetaVive);

        btnNext_01=(Button)findViewById(R.id.btnNext_01);
        btnNext_01.setOnClickListener(this);

    }

    public void onClick(View v){

        Intent it = new Intent(this, Main2Activity.class);
        it.putExtra("planetaVive", etPlantaVive.getText().toString());
        startActivity(it);

    }

}

I export the plaintext contents to Main2activity using the code it.putExtra("planetaVive", etPlantaVive.getText().toString());

In the Main2activity has a plaintext where the user informs which planet he would like to live on.

inserir a descrição da imagem aqui

Main2activity.java.

package genesysgeneration.third;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Main2Activity extends AppCompatActivity implements View.OnClickListener{

    private String planetaVive;
    private EditText etPlanetaDesejo;
    private Button btnNext_02;

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

        planetaVive = getIntent().getStringExtra("planetaVive");

        etPlanetaDesejo=(EditText)findViewById(R.id.etPlanetaDesejo);
        btnNext_02=(Button)findViewById(R.id.btnNext_02);
        btnNext_02.setOnClickListener(this);

    }

    public void onClick(View v){

        Intent it = new Intent(this, Main3Activity.class);
        it.putExtra("planetaVive", planetaVive);
        it.putExtra("planetaDesejo", etPlanetaDesejo.getText().toString());
        startActivity(it);

    }

}

I wish I didn’t have to import the contents of the first plaintext (etPlanetaVive) => planetaVive = getIntent().getStringExtra("planetaVive"); I would like to do this only in the third (Main3activity).

In Main3activity appears this:

inserir a descrição da imagem aqui

Main3activity.java.

package genesysgeneration.third;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

public class Main3Activity extends AppCompatActivity {

    private String planetaVive, planetaDesejo;
    private TextView tvFinalPlanetaVive, tvFinalPlanetaDesejo;

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

        planetaVive = getIntent().getStringExtra("planetaVive");
        planetaDesejo = getIntent().getStringExtra("planetaDesejo");

        tvFinalPlanetaVive=(TextView)findViewById(R.id.tvFinalPlanetaVive);
        tvFinalPlanetaVive.setText(tvFinalPlanetaVive.getText().toString() + planetaVive);

        tvFinalPlanetaDesejo=(TextView)findViewById(R.id.tvFinalPlanetaDesejo);
        tvFinalPlanetaDesejo.setText(tvFinalPlanetaDesejo.getText().toString() + planetaDesejo);

    }
}

As you saw, I had to move the contents of the first Activity to the second, only then to the third.

1 answer

1

Just save it all in a static class.

public class Dados{

            public String txtAct1 = null;
            public static Dados instancia = null;

    public Dados getInstancia(){
           if(instancia==null){
           instancia = new Dados();
        }
           return instancia;
    }

    public void setTxtAct1(String txtAct1 ){
        this.txtAct1 = txtAct1;
    }

    public String getTxtAct1(){
        return txtAct1;
    }
 }

Then just go stocking the data with Dados.getInstancia.setTxtAct1("terra"); (logically Voce will create the amount of strings you want and your respective gets and sets) and recover with String act1 = Dados.getInstancia.getTxtAct1;

Browser other questions tagged

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