-2

I am reading User class from database with this code :

           mDatabase.orderByChild("name")
                    .equalTo(s1)
                    .addValueEventListener(new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            for (DataSnapshot userSnapshot : dataSnapshot.getChildren()) {

                                    User user = userSnapshot.getValue(User.class);

                                }
                            }

I want to display content of User object in a new activity. How do I pass User object to new activity?

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

You Can Transfer Data Using Following Way:

  1. Passing Data Through Intent(based on Key value or In Bundle Format)
  2. Using Interface
  3. Creating a Method in Another Activity and Call This method From First Activity (Using Public Method)
Nimesh Patel
  • 1,394
  • 13
  • 26
  • 1
    your answer doesn't actually tell OP how to do any of these, `Using Interface` doesn't actually mean anything to anyone reading this in future – a_local_nobody Feb 13 '20 at 12:38
  • I answer as per qn ask by user and actual qn is what are the ways of trasfer data. so i mention the diffrent ways through which you can passdata from one activity to another and we all know it's core functionality of interface. – Nimesh Patel Feb 13 '20 at 13:12
  • for that they can use intent. see the qn first. here qn is about to list out ways to transfer data. If qn ask about how to implement this task than my answer will be in depth. and if yor still new in android than you can use intent which i mention as in answer. – Nimesh Patel Feb 13 '20 at 14:09
0

Using Intent, you can share data between two activities. In your case, you have to make user object parcelable.

  1. Implement Parcelable interface in your user class.

  2. Send the user object in intent like intent.putExtra("user", userObject)

  3. In your second activity get user object from intent :

User user = (User) getIntent().getExtras().getParcelable("user")