1

I'm building an app with 2 fragments.

  • 1st Fragment: ImageView and 2 TextViews
  • 2nd Fragment: 2 EditTexts and 3 Buttons

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.

  • Does the onRecieve method get called at all? And what is the content of the intent when it does receive the broadcast – Brandon Feb 11 '19 at 16:35
  • @Brandon I've updated my answer with the intent. And about `onReceive`. When I click the "Save" button in the 2nd fragment, I want the download to happen right away. Because this is my first time working with LBMs, I'm not sure how to call `onReceive` – Andros Adrianopolos Feb 11 '19 at 16:38

1 Answers1

0

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);
Marat
  • 6,142
  • 6
  • 39
  • 67
  • I get the `unRegisterReceiver` part but why the first code and why in `onResume`? – Andros Adrianopolos Feb 11 '19 at 17:15
  • You need to register receiver to be able to get broadcasts. Why to register in `onResume` is just on of the options. It mostly depends on the design of your app and its needs. I guess this discussion describes best all cases of registering and unregistering receiver: https://stackoverflow.com/q/8802157/6272369 – Marat Feb 11 '19 at 17:40
  • `Constants.INTENT_NEW_MESSAGE` is not recognized in a fragment. – Andros Adrianopolos Feb 11 '19 at 17:46
  • Yes, you are right. It should be exact string that you are giving to intent as an action. I have edited my answer. – Marat Feb 11 '19 at 17:47
  • I've accepted your answer but can't upvote yet due to my rep level. If you think that this was a well-asked question, could you give me an upvote as well? – Andros Adrianopolos Feb 11 '19 at 21:24