Button to return to an attempt

0

My problem is: Return from an Activity to an intent.

I have the following sequence of pages:

-MainActivity
-Intent(Escolher foto)
-DadosActivity

In MainActivity I have a button to open an intent for the user to 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 attempt the user will select a photo and after that it is sent that 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, DataActivity, the user can make some changes to the image, but my problem is when pressing the back button of the device and / or the 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 in AndroidManifest

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

Activity is returning to MainActivity, but I wanted to press it to return to the intent and to choose the image again.

Does anyone have any suggestions on how to do this, so that instead of returning to MainActivity, back to try so he can choose the image again?

    
asked by anonymous 23.10.2018 / 12:51

1 answer

1

How about overwriting the onBackPressed() method in DadosActivity and putting the call back to Intent there? This method is called when you press back and by default it terminates the current Activity. Since you're getting back to your first one, it means that Intent does not go into the Activities stack so you would not be able to get back 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 code MainActivity you will need some changes. You would have to receive a message from DadosActivity when you come back and make a treatment so when you receive this message make direct the code to choose image. To do this use startActivityForResult to call DadosActivity and in DadosActivity you use setResult with a specific code. In MainActivity in method onActivityResult , which is returned after DadosActivity is not returned, you treat this code and call Intent for photo choice. Give a little work but I think it will get better.

    
23.10.2018 / 13:14