Sending data between Activitys with Putextra - Management

Asked

Viewed 119 times

0

I have the following problem: I have three screens:

  • Main
  • Activity 2
  • Activity 3

In the first Activity (Main) I send a data by putExtra to Activity 2 (id user). Activity 2 is the profile screen, and at a certain point I send to a third Activity (ex: photo preview screen).

Even then it works with sending Putextra and Getextra. The problem is when I click the back button, is giving an error and stopping the application.

I’ve even searched and tried to implement something like startActivityForResult, but I didn’t understand it right and it doesn’t work.

The problem is specifically in the return of Activity 3 to Activity2, because even I try to send the user code again to this screen is giving conflict, because Activity 2 already has at its beginning a Getextra, which in turn came from Activity Main.

Example:

Activity Main:

public class MainActivity extends AppCompatActivity {

    private Button btnIrAct2;


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

        btnIrAct2 = (Button) findViewById(R.id.btnIrAct2);
        btnIrAct2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent2 = new Intent(MainActivity.this, Activity2.class);
                intent2.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent2.putExtra("id_empresa", "12345");
                startActivity(intent2);

            }
        });


    }
}

Activity 2:

public class Activity2 extends AppCompatActivity {

    private Button btnIrAct3;
    private String mId_Empresa = null;
    static final int SERVICO_DETALHES_REQUEST = 1;
    private TextView tvResultado;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        /*  Recebe id de outra tela*/
        mId_Empresa = getIntent().getExtras().getString("id_empresa");

        tvResultado = (TextView) findViewById(R.id.tvAct2);
        tvResultado.setText(mId_Empresa);

        btnIrAct3 = (Button) findViewById(R.id.btnIrAct3);
        btnIrAct3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                Intent intent3 = new Intent(Activity2.this, Activity3.class);
                intent3.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                intent3.putExtra("id_empresa", "12345");
                startActivityForResult(intent3, SERVICO_DETALHES_REQUEST );

            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {
        if (requestCode == SERVICO_DETALHES_REQUEST && resultCode == RESULT_OK) {
            String resultBack = data.getStringExtra("id_empresa");
        }
    }

}

Activity 3:

public class Activity3 extends AppCompatActivity {

    private String idEmpresa = null;
    private TextView resultado;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_3);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        /*  Recebe id de outra tela*/
        idEmpresa = getIntent().getExtras().getString("id_empresa");

        resultado = (TextView) findViewById(R.id.tvAct3);
        resultado.setText(idEmpresa);


    }

    @Override
    public void finish()
    {
        Intent data = new Intent();
        data.putExtra("id_empresa", "12345");
        setResult(Activity.RESULT_OK, data);
        super.finish();
    }

}

I need to resolve this conflict. Send the same data as Activity 3 Putextra for Activity 2, but Activity 2 already expects a Getextra from Activity Main.

1 answer

0

You’re putting the flag FLAG_ACTIVITY_CLEAR_TOP in your attempts to open activities 2 and 3. By placing this flag, you remove previous activities from your stack. If this is not the desired behavior, you have to remove this flag(FLAG_ACTIVITY_CLEAR_TOP) and then you won’t need the startActivityForResult() from Activity 2 to 3.

More details on: FLAG_ACTIVITY_CLEAR_TOP

Browser other questions tagged

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