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

0

I'd like to know how to pass the content of a plainText that is in the MainActivity direct to Main3Activity, without having to also import to Main2Activity and then to Main3Activity.

The user passes Main2Activity before going to Main3Acitvity.

MainActivity has a plainText (etPlanetaVive) where the user informs the planet where he lives.

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 contents of plainText to Main2Activity using the code it.putExtra("planetaVive", etPlantaVive.getText().toString());

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

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 would like to not have to import to the second activity the contents of the first plainText (etPlanetaVive) = > planetaVive = getIntent().getStringExtra("planetaVive"); I would like to have to do this only in the third (Main3Activity).

In Main3Activity this appears:

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 seen, I had to pass the contents of the first activity to the second, only to move on to the third.

    
asked by anonymous 21.01.2017 / 22:18

1 answer

1

Just save everything 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;
    }
 }

You can only do this by storing the data with Dados.getInstancia.setTxtAct1("terra"); (logically you will create the amount of strings you want and your gets and sets) and recover with String act1 = Dados.getInstancia.getTxtAct1;

    
23.01.2017 / 13:43
Help with JS code File upload with php validation [duplicate]