Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - Activity seems to not get called after the intent returns

Tags:

I have an activity where you can click on a button that sends you to an activity where you can add pictures (using startActivityForResult). You can add a couple of pictures. Every picture you add, gets converted to a byte[] array, and gets stored as an extra in the result intent. When the second activity finishes, if i added one picture, and finish the activity, everything is fine, it returns to the previous activity with the button. If i add two or more pictures, and finish the second activity, the previous activity doesn't even get called. It takes me directly to an activity before the activity with the button, but first i see like a black screen that seems like the previous activity crashed, though no errors can be found in the logs. The onActivtiyResult in the previous activty does not get called as well. Any reason why this might be happening ?

Code sending intent to photo adding activity :

Intent addEditPicturesIntent = new Intent(getApplicationContext()
                        ,AddOrEditPicturesOnProductActivity.class);
                startActivityForResult(addEditPicturesIntent,33);

Code finishing the photo adding activity:

Intent returnIntent=new Intent();
                if (hashMap.containsKey("one")){
                    Log.d(TAG,"Submit Button clicked and now putting extra in intent-->1");
                    returnIntent.putExtra("1",hashMap.get("one"));

                }
                if (hashMap.containsKey("two")){
                    Log.d(TAG,"Submit Button clicked and now putting extra in intent-->2");

                    returnIntent.putExtra("2",hashMap.get("two"));
                }
                if (hashMap.containsKey("three")){
                    Log.d(TAG,"Submit Button clicked and now putting extra in intent-->3");

                    returnIntent.putExtra("3",hashMap.get("three"));
                }
                setResult(Activity.RESULT_OK, returnIntent);
                finish();

onActivityResult code in the activity with the button:

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.d(TAG,"onAcitivtyResult"); // <--- NOT EVEN THIS GETS CALLED when returning

        if (//uninmportant stuff){
        //Other uninporant stuff
        else if(requestCode == 33){
            Log.d(TAG,"[Test Return Code returned]!");
            Log.d(TAG,data.getExtras().toString());
            Bundle bundle=data.getExtras();
            List<ProductImage> productImageList=new ArrayList<>();
            for (Integer i=1;i<=3;i++){
               if ( bundle.getByteArray(i.toString())!=null){
                   ProductImage productImage=new ProductImage();
                   productImage.setImageBytes(bundle.getByteArray(i.toString()));
                   productImageList.add(productImage);
               }
            }
            addProductViewModel.fillUpMutableLiveData(productImageList);
            addProductViewModel.updateCurrentId(0);
        }
    }
like image 450
AskQuestions Avatar asked May 09 '19 11:05

AskQuestions


1 Answers

Every picture you add, gets converted to a byte[] array, and gets stored as an extra in the result intent.

That is a really bad idea.

though no errors can be found in the logs

You are probably getting "FAILED BINDER TRANSACTION" warnings.

Any reason why this might be happening ?

My guess is that you are attempting to pass too much data via IPC. The Intent that you supply to setResult() is passed from your app to a core OS process, then back to your app and the caller of startActivityForResult(). At best, you can pass 1MB in an IPC transaction, and depending on what else is going on at the time, the limit may be substantially lower.

Either:

  • Have one activity, not two, by using fragments for the individual bits of UI and using a shared ViewModel to expose the results of one fragment to another; or

  • Carefully hold onto these images in a shared cache, so you are passing cache keys as extras, not byte[]; or

  • Find some other architecture that you like that does not involve putting large byte[] values into an Intent for use with startActivity(), startActivityForResult(), or setResult()

like image 124
CommonsWare Avatar answered Sep 20 '22 17:09

CommonsWare