androidandroid-fragmentsbroadcastreceiverandroid-broadcastlocalbroadcastmanager

Invoking a method in a fragment through a LocalBroadcastManager


I'm building an app with 2 fragments.

In the 2nd Fragment, I have a "Save" Button. In the onClick of this button, I am trying to send a local broadcast:

btnSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                localBroadcastManager.sendBroadcast(localIntent);
            }
        });

And this is my intent:

 final LocalBroadcastManager localBroadcastManager = LocalBroadcastManager
                .getInstance(Objects.requireNonNull(getContext()));

        final Intent localIntent = new Intent("CUSTOM_ACTION");

The point of this is so that the 1st fragment picks it up and executes a method to save the Image as a bitmap with its captions (that's what the 2 TextViews are for). This is my attempt:

private BroadcastReceiver listener = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent ) {
            String data = intent.getStringExtra("DATA");
            Toast.makeText(context, data + " received", Toast.LENGTH_SHORT).show();

            createAndSaveBitmap(topTextView.getText().toString(), bottomTextView.getText().toString(), memeBitmap, memeCanvas, memePaint, imageView, imageUri);
        }
    };

    public void createAndSaveBitmap(String top, String bottom, Bitmap bitmap,
                                    Canvas canvas, Paint paint, ImageView imageView, Uri imageUri) {

        try {
            memeBitmap = BitmapFactory.decodeStream(context.getContentResolver().openInputStream(imageUri))
                    .copy(Bitmap.Config.ARGB_8888, true); }

        catch (FileNotFoundException e) { e.printStackTrace(); }

        canvas = new Canvas(memeBitmap);

        paint = new Paint();

        canvas.drawText(top, 0, 0, paint);
        canvas.drawText(bottom, 0, memeCanvas.getHeight() - 10, paint);

        imageView.setImageBitmap(memeBitmap);

        if(bitmap != null){
            File file = Environment.getExternalStorageDirectory();
            File newFile = new File(file, "test.jpg");

            try {
                FileOutputStream fileOutputStream = new FileOutputStream(newFile);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
                fileOutputStream.flush();
                fileOutputStream.close();
            }

            catch (FileNotFoundException e) {e.printStackTrace();}
            catch (IOException e) { e.printStackTrace(); }
        }
    }

When I run the app, nothing happens when I click the "Save" button. Doesn't download the image, nor does it crash. Since there is no error stacktrace, I can't see where I'm going wrong.


Solution

  • Make sure that you are calling this in your receiving fragment's onResume.

    LocalBroadcastManager.getInstance(getActivity()).registerReceiver(
            listener, 
            new IntentFilter("CUSTOM_ACTION")
    );
    

    and this in onPause

    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(listener);