Button to return to an Intent

Asked

Viewed 169 times

0

My problem is: Back from an Activity to an Internet.

I have the following sequence of pages:

-MainActivity
-Intent(Escolher foto)
-DadosActivity

In Mainactivity I have a button to open an Intent so that the user select a photo, with the following code:

Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

if (i.resolveActivity(MainActivity.this.getPackageManager()) != null) {

    startActivityForResult(i, SELECAO_GALERIA);

}

In this Input the user will select a photo and after that is sent this selected item to another page, through this code:

   @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == MainActivity.this.RESULT_OK) {

        Bitmap image = null;

        try {

            switch (requestCode) {

                case SELECAO_CAMERA:

                    image = (Bitmap) data.getExtras().get("data");

                    break;

                case SELECAO_GALERIA:

                    Uri localImagemSelecionada = data.getData();

                    image = MediaStore.Images.Media.getBitmap(MainActivity.this.getContentResolver(), localImagemSelecionada);

                    break;

            }

            if (image != null) {

                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                image.compress(Bitmap.CompressFormat.JPEG, 70, baos);
                byte[] dadosImagem = baos.toByteArray();

                Intent i = new Intent(MainActivity.this, DadosPostagemActivity.class);
                i.putExtra("fotoEscolhida", dadosImagem);
                startActivity(i);


            }

        } catch (Exception e) {

            e.printStackTrace();

        }

    }
}

In the last Activity, Dadosactivity, the user can make some changes to the image, but my problem is to press the button on the back of the device and/or Toolbar, with the following code:

toolbar = (Toolbar) findViewById(R.id.tb_dados);
toolbar.setTitle("Adicione uma descrição");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);

According to what I did on Androidmanifest

<activity
android:name=".activity.DadosPostagemActivity"
android:parentActivityName=".activity.MainActivity" />

Activity is returning to Mainactivity, but I wanted that by pressing back on the Internet and I could choose the image again.

Does anyone have any suggestions on how to do this, so instead of going back to Mainactivity, go back to the Internet so he can choose the image again?

1 answer

1


How about overriding the method onBackPressed() in DadosActivity and put there again the call to the Intent? This method is called when you press back and by default it terminates the current Activity. As it is returning to its first one, it means that the Intent does not enter the pile of Activities, so there would be no way to return to it naturally, without a new call.

@Override
public void onBackPressed() {
    super.onBackPressed(); //comportamento padrão - finaliza Activity atual
    //chamada para o Intent
}

Edit: To reuse code as you asked in the comments, the simplest option would be to create a class with the code that is used by the two Activities and reuse. But if you want to take advantage of the MainActivity will require some changes. You would have to receive a message from DadosActivity when you return and do a treatment for when you receive this message make direct the code to choose image. For this use startActivityForResult to call the DadosActivity and in the DadosActivity you use setResult with a specific code. In the MainActivity in the method onActivityResult, which is returned after the completion of the DadosActivity in the return, you treat this code and make the call of Intent for the choice of photo. Gives a little work but I think q will look better.

  • I’ll test here and give you a feedback! Vlww

  • The only problem that I found is that, I will need to do again the code that passes the chosen image to the Data screen. If you have a way to go back to Mainactivity but with the button already the action_pick Intent already open ai n need to redo these code. would have some way?

  • I edited the answer. See if it fits what you need!

Browser other questions tagged

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