从另一个应用程序启动特定片段

时间:2019-12-05 14:31:39

标签: android firebase android-fragments share

我有一个片段,当用户选择从其他应用程序共享图像时应启动该片段。下面是我的片段代码。据我所知,您可以通过在具有MIME类型的清单文件中指定活动来启动活动。但是片段呢?

我的应用程序是使用Firebase的简单聊天应用程序。

问题是我希望用户完成以下步骤:

1。从其他应用程序中选择图片,然后在共享菜单中单击我的应用程序。

2。然后弹出“朋友”片段(如何实现?)

3。当用户单击一个朋友时,Friends片段将朋友作为列表视图包含在列表中,然后将所选图像发送给他们。(以whatsapp的形式)

Friends.java

public class Friends extends Fragment {

private RecyclerView rv;
private DatabaseReference dr,drUsers;
private FirebaseAuth auth;
private String user;


FirebaseRecyclerAdapter<FriendsData,FriendsViewHolder> frv;




private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

private OnFragmentInteractionListener mListener;

public Friends() {
    // Required empty public constructor
}

public static Friends newInstance(String param1, String param2) {
    Friends fragment = new Friends();
    Bundle args = new Bundle();
    args.putString(ARG_PARAM1, param1);
    args.putString(ARG_PARAM2, param2);
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_friends, container, false);
    rv = (RecyclerView)view.findViewById(R.id.friendsView);
    auth = FirebaseAuth.getInstance();
    user = auth.getCurrentUser().getUid();
    dr = FirebaseDatabase.getInstance().getReference().child("Friends_Formed").child(user);
    drUsers = FirebaseDatabase.getInstance().getReference().child("Chat_Profiles");
    dr.keepSynced(true);
    drUsers.keepSynced(true);
    rv.setHasFixedSize(true);
    rv.setLayoutManager(new LinearLayoutManager(getContext()));

    //Uri imageUri = (Uri) getActivity().getIntent().getExtras().get(Intent.EXTRA_STREAM);
    //System.out.println("IMAGE URI ISSSSSSSSSS "+ imageUri);


    // Inflate the layout for this fragment
    return view;
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
    if (mListener != null) {
        mListener.onFragmentInteraction(uri);
    }
}


@Override
public void onStart() {
    super.onStart();


    Query personsQuery = dr.orderByKey();

    FirebaseRecyclerOptions<FriendsData> options =
            new FirebaseRecyclerOptions.Builder<FriendsData>().setLifecycleOwner(this)
                    .setQuery(personsQuery, FriendsData.class)
                    .build();

    frv = new FirebaseRecyclerAdapter<FriendsData, FriendsViewHolder>(options) {
        @Override
        protected void onBindViewHolder(@NonNull final FriendsViewHolder holder, int position, @NonNull FriendsData model) {
                holder.setDate(model.getDate());
                holder.setImage(model.getThumb_image());

                final String listUsers = getRef(position).getKey();

            if (!listUsers.equals(null))

                drUsers.child(listUsers).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                        final String username = dataSnapshot.child("name").getValue().toString();
                        String thumb = dataSnapshot.child("thumb_image").getValue().toString();
                        //String online  = dataSnapshot.child("onlineStatus").getValue().toString();

                        if(dataSnapshot.hasChild("onlineStatus")){
                            String userOnline = dataSnapshot.child("onlineStatus").getValue().toString();
                            holder.setOnlineStatus(userOnline);
                        }

                        holder.setName(username);
                        holder.setImage(thumb);
                        //holder.setOnlineStatus(online);

                        holder.view.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View view) {
                                CharSequence actions[] = new CharSequence[]{"Open "+username+"'s Profile","Chat with "+username};
                                AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
                                builder.setTitle("Select an Action");
                                builder.setItems(actions, new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialogInterface, int i) {
                                        if (i == 0) {
                                            Intent intent = new Intent(getContext(), ViewProfile.class);
                                            intent.putExtra("id", listUsers);
                                            startActivity(intent);
                                        } else {

                                            Intent intent = new Intent(getContext(), ChatActivity.class);
                                            intent.putExtra("id", listUsers);
                                            intent.putExtra("user_name", username);
                                            startActivity(intent);
                                        }
                                    }
                                });
                                builder.show();
                            }
                        });


                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError databaseError) {

                    }
                });

        }

        @NonNull
        @Override
        public FriendsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.friends_status, parent, false);

            return new FriendsViewHolder(view);
        }
    };
    rv.setAdapter(frv);

}

public static class FriendsViewHolder extends RecyclerView.ViewHolder{

    View view;
    public FriendsViewHolder(View itemView) {
        super(itemView);

        view = itemView;
    }

    public void setDate(String date){
        TextView username = (TextView)view.findViewById(R.id.status2);
        username.setText(date);
    }

    public void setImage(String image){
        CircleImageView thumb = (CircleImageView)view.findViewById(R.id.circleImageView2);
        Picasso.get().load(image).placeholder(R.drawable.default_avatar).into(thumb);

    }

    public void setName(String name){
        TextView username = (TextView)view.findViewById(R.id.name2);
        username.setText(name);
    }

    public void setOnlineStatus(String onlineStatus){
        ImageView image = (ImageView)view.findViewById(R.id.onlineStatus);

        if(onlineStatus.equals("true")){
            image.setVisibility(View.VISIBLE);
        }else{
            image.setVisibility(View.INVISIBLE);
        }
    }



}

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    if (context instanceof OnFragmentInteractionListener) {
        mListener = (OnFragmentInteractionListener) context;
    } else {
        throw new RuntimeException(context.toString()
                + " must implement OnFragmentInteractionListener");
    }
}

@Override
public void onDetach() {
    super.onDetach();
    mListener = null;
}


public interface OnFragmentInteractionListener {
    // TODO: Update argument type and name
    void onFragmentInteraction(Uri uri);
}

}

0 个答案:

没有答案