I can't send data to a fragment from an activity with broadcast

Scepticul :

I'm trying to send data from a an activity to a fragment.The data is the name of the item witch is currently clicked, I needed because I want to make a query.But the data is never sent. I also tried to send the broadcast before calling fragment.begingTrascation, but still doesn't worked, I also changed the name of the constants but still, the broadcast from the Fragment is never triggered. I don't know why is not working, what can I do to solve this?

    private NavigationView.OnNavigationItemSelectedListener navigationListener = new NavigationView.OnNavigationItemSelectedListener() {
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.nav_today) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new TodayFragment())
                    .commit();

        } else if (item.getItemId() == R.id.nav_inbox) {
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new InboxFragment())
                    .commit();

        } else if (item.getItemId() == R.id.nav_add_list) {
            ListBottomSheet bottomSheet = new ListBottomSheet();
            bottomSheet.showNow(getSupportFragmentManager(), "bottomSheetList");

        } else if (item.getItemId() == R.id.nav_account) {
            startActivity(new Intent(HomeDrawerActivity.this, AccountActivity.class));

        } else if (item.getItemId() == R.id.nav_random_task) {
            startActivity(new Intent(HomeDrawerActivity.this, RandomTaskActivity.class));

        } else {
            /* one of the list items
              was clicked therefore
              get the data form db
             */
            item.setCheckable(true);
            item.setChecked(true);
            String listName = item.getTitle().toString();
            Toast.makeText(HomeDrawerActivity.this, listName, Toast.LENGTH_LONG).show();

            /* create the fragment */

            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragment_container, new ListFragment())
                    .commit();

            /* send data to the fragment */

            Intent intent = new Intent(LIST_ACTION);
            intent.putExtra(KEY_LIST_NAME, listName);

            LocalBroadcastManager.getInstance(HomeDrawerActivity.this).sendBroadcast(intent);

        }

        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
};

public class ListFragment extends Fragment {
private Context context;
private String documentName;

private ListAdaptor listAdaptor;
private RecyclerView recyclerViewList;

private ColorDrawable swipeBackgroundRight = new ColorDrawable(Color.parseColor("#FF0000"));
private ColorDrawable swipeBackgroundLeft = new ColorDrawable(Color.parseColor("#1E88E5"));

private Drawable iconDelete;
private Drawable iconComplete;

private SoundPool soundPool;
private int soundId;

private FirebaseFirestore firestore = FirebaseFirestore.getInstance();
private CollectionReference collectionReference = firestore.collection("Users")
        .document(FirebaseAuth.getInstance().getCurrentUser().getUid())
        .collection("Lists");

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.list_fragment, container, false);

    LocalBroadcastManager.getInstance(context)
            .registerReceiver(broadcastReceiver, new IntentFilter(HomeDrawerActivity.LIST_ACTION));


    iconDelete = ContextCompat.getDrawable(getActivity(), R.drawable.ic_delete);
    iconComplete = ContextCompat.getDrawable(getActivity(), R.drawable.ic_completed);

    if (!TextUtils.isEmpty(documentName)) {
        collectionReference.document(documentName);

    } else {
        Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
    }

     /* create the option query
        for the recycler
      */

    Query query = collectionReference;

    FirestoreRecyclerOptions<Task> options =
            new FirestoreRecyclerOptions.Builder<Task>()
                    .setQuery(query, Task.class)
                    .build();

    listAdaptor = new ListAdaptor(options);

    recyclerViewList = rootView.findViewById(R.id.recycler_view_list);
    recyclerViewList.setHasFixedSize(true);
    recyclerViewList.setLayoutManager(new LinearLayoutManager(getActivity()));
    recyclerViewList.setAdapter(listAdaptor);


    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        AudioAttributes audioAttributes =
                new AudioAttributes.Builder()
                        .setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION)
                        .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
                        .build();

        soundPool = new SoundPool.Builder()
                .setMaxStreams(1)
                .setAudioAttributes(audioAttributes)
                .build();

    } else {
        soundPool = new SoundPool(1, AudioManager.STREAM_RING, 0);
    }
    soundId = soundPool.load(getActivity(), R.raw.completed, 1);

    new ItemTouchHelper(new ItemTouchHelper.SimpleCallback(0, ItemTouchHelper.RIGHT | ItemTouchHelper.LEFT) {
        @Override
        public boolean onMove(@NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, @NonNull RecyclerView.ViewHolder target) {
            return true;
        }

        @Override
        public void onSwiped(@NonNull RecyclerView.ViewHolder viewHolder, int direction) {
            if (direction == ItemTouchHelper.RIGHT) {
                listAdaptor.delete(viewHolder.getAdapterPosition());
                Toast.makeText(context, "Task deleted", Toast.LENGTH_SHORT).show();
            } else if (direction == ItemTouchHelper.LEFT) {
                listAdaptor.update(viewHolder.getAdapterPosition());

                soundPool.play(soundId, 1, 1, 1, 0, 1);

                Toast.makeText(context, "Task completed", Toast.LENGTH_SHORT).show();
                swipeBackgroundRight.setBounds(0, 0, 0, 0);
                iconDelete.setBounds(0, 0, 0, 0);
            }
        }

        @Override
        public void onChildDraw(@NonNull Canvas c, @NonNull RecyclerView recyclerView, @NonNull RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {

            View itemView = viewHolder.itemView;
            int iconMargin = (itemView.getHeight() - iconDelete.getIntrinsicHeight()) / 2;
            if (dX > 0) {
                swipeBackgroundRight.setBounds(itemView.getLeft(), itemView.getTop(), (int) dX, itemView.getBottom());
                iconDelete.setBounds(itemView.getLeft() + iconMargin, itemView.getTop() + iconMargin, itemView.getLeft() + iconMargin + iconDelete.getIntrinsicWidth(), itemView.getBottom() - iconMargin);
            } else if (dX < 0) {
                swipeBackgroundLeft.setBounds(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
                iconComplete.setBounds(itemView.getRight() - iconMargin - iconComplete.getIntrinsicWidth(), itemView.getTop() + iconMargin, itemView.getRight() - iconMargin,
                        itemView.getBottom() - iconMargin);

            } else {
                swipeBackgroundLeft.setBounds(0, 0, 0, 0);
                swipeBackgroundRight.setBounds(0, 0, 0, 0);

            }
            c.save();

            swipeBackgroundRight.draw(c);
            swipeBackgroundLeft.draw(c);
            if (dX > 0) {
                c.clipRect(itemView.getLeft(), itemView.getTop(), (int) dX, itemView.getBottom());
            } else {
                c.clipRect(itemView.getRight() + (int) dX, itemView.getTop(), itemView.getRight(), itemView.getBottom());
            }

            iconDelete.draw(c);
            iconComplete.draw(c);
            c.restore();


            super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);

        }
    }).attachToRecyclerView(recyclerViewList);

    return rootView;
}

@Override
public void onAttach(@NonNull Context context) {
    super.onAttach(context);
}

private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        if (HomeDrawerActivity.LIST_ACTION.equals(intent.getAction())) {

            /* get the data from db */

            documentName = intent.getStringExtra(HomeDrawerActivity.KEY_LIST_NAME);
            Toast.makeText(getActivity(), "List fragment" + documentName, Toast.LENGTH_LONG).show();

        }
    }
};

@Override
public void onStart() {
    super.onStart();
    listAdaptor.startListening();
}

@Override
public void onDestroyView() {
    super.onDestroyView();
    LocalBroadcastManager.getInstance(context).unregisterReceiver(broadcastReceiver);
}

}

Bö macht Blau :

If you send a broadcast before the Fragment transaction is started, it is very likely that the Fragment will not manage to set up its BroadcastReceiver in time to catch the transmission.

But in your case, you can add the data you want to pass to the Fragment to the arguments Bundle of the ListFragment instance before starting the transaction:

String listName = item.getTitle().toString();

/* create the fragment */
ListFragment listFragment = new ListFragment();

/* store data in arguments Bundle */
Bundle args = new Bundle();
args.putString(KEY_LIST_NAME, listName);
listFragment.setArguments(args);

getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragment_container, listFragment )
                .commit();

In ListFragment, you can access the data as follows:

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = LayoutInflater.from(getActivity()).inflate(R.layout.list_fragment, container, false);
    Bundle args = getArguments();
    String listName = args.getString(KEY_LIST_NAME);

    // ....
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=336702&siteId=1