title: Android - Pass an intent from one activity to another that implements a callback class with Firebase
body: <p>I have two activities: <code>AddUser</code> and <code>ToDo</code>. <code>ToDo</code> implements a class with callback. <code>ToDo</code> allows the user to create a to do list, and the to do items will be displayed instantly in a recyclerView. User can add, update, or delete to do items in <code>ToDo</code>.</p>
  
  <p>AddUser.java</p>
  
  <pre><code>public class AddUser extends AppCompatActivity implements View.OnClickListener{
  
  private DatabaseReference mUserRef;
  
  @Override
  protected void onCreate(Bundle savedInstanceState){
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_add_user);
  
  mUserRef = FirebaseDatabase.getInstance().getReference().child("users");
  
  EditText etUserid = (EditText) findViewById(R.id.etUserid);
  EditText etUsername = (EditText) findViewById(R.id.etUsername);
  Button btnNext = (Button) findViewById(R.id.btnNext);
  
  btnNext.setOnClickListener(this);
  }
  
  public void addUser(UserDetails userDetails){
  userPushKey = mUserRef.push().getKey();
  mUserRef.child(userPushKey).setValue(userDetails);
  }
  
  @Override
  public void onClick(View v){
  if(v == btnNext){
  String inputUserid = etUserid.getText().toString();
  String inputUsername = etUsername.getText().toString();
  
  addUser(new UserDetails(inputUserid, inputUsername));
  
  Intent intent = new Intent(AddUser.this,ToDo.class);
  intent.putExtra("userKeyRef", userPushKey);
  startActivity(intent);
  }
  }
  }
  </code></pre>
  
  <p>ToDo.java</p>
  
  <pre><code>public class ToDo extends AppCompatActivity implements UserTodoAdapter.Callback {
  
  private UserTodoAdapter mAdapter;
  
  @Override
  protcted void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_todo);
  
  mAdapter = new UserTodoAdapter(this);
  RecyclerView view = (RecyclerView) findViewById(R.id.recycler_view);
  view.setHasFixedSize(true);
  view.setAdapter(mAdapter);
  }
  
  @Override
  public void onEdit(final UserTodo userTodo){
  // some functions here
  }
  }
  </code></pre>
  
  <p>UserTodoAdapter.java</p>
  
  <pre><code>public class UserTodoAdapter extends RecyclerView.Adapter&lt;UserTodoAdapter.ViewHolder&gt; {
  private List&lt;UserTodo&gt; mUserTodo;
  private Callback mCallback;
  private DatabaseReference mUserTodoRef;
  
  public UserTodoAdapter(Callback callback) {
  mCallback = callback;
  mUserTodo = new ArrayList&lt;&gt;();
  
  // need to get the push key from AddUser activity
  mUserTodoRef = FirebaseDatabase.getInstance.getReference().child(users).child("Need the push key here").child("todo");
  mUserTodoRef.addChildEventListener(new TodoChildEventListener());
  }
  
  private class TodoChildEventListener implements ChildEventListener{
      @Override
      public void onChildAdded(DataSnapshot dataSnapshot, String s){
          // action here
      }
  
      @Override
      public void onChildChanged(DataSnapshot dataSnapshot, String s){
          // action here
      }
  
      @Override
      public void onChildRemoved(DataSnapshot dataSnapshot){
          // action here
      }
  
      @Override
      public void onChildMoved(DataSnapshot dataSnapshot, String s){
          // action here
      }
  
      @Override
      public void onCancelled(DatabaseError databaseError){
          // action here
      }
  }
  
  @Override
  public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
      View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.a_custom_view, parent, false);
      return new ViewHolder(view);
  }
  
  @Override
  public void onBindViewHolder(ViewHolder holder, final int position){
      final UserTodo userTodo = mUserTodo.get(position);
      holder.mTodoTitle.setText(userTodo.getTodoTitle());
      holder.mTodoDesc.setText(userTodo.gerTodoDesc());
      holder.itemView.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View v) {
              mCallback.onEdit(userTodo);
          }
      });
  
      holder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
          @Override
          public boolean onLongClick(View v) {
              removeTodo(mUserTodo.get(position));
              return true;
          }
      });
  }
  
  @Override
  public int getItemCount(){
      return mUserTodo.size();
  }
  
  public interface Callback{
      public void onEdit(UserTodo userTodo);
  }
  
  class ViewHolder extends RecyclerView.ViewHolder{
      private TextView mTodoTitle;
      private TextView mTodoDesc;
  
      public ViewHolder(View itemView){
          super(itemView);
          mTodoTitle = (TextView) itemView.findViewById(R.id.tvTodoTitle);
          mTodoDesc = (TextView) itemView.findViewById(R.id.tvTodoDesc);
      }
  }
  
  public void addTodo(UserTodo userTodo){
      mUserTodoRef.push().setValue(userTodo);
  }
  
  public void updateTodo(UserTodo userTodo, String newTodoTitle, String newTodoDesc){
      userTodo.setTodoTitle(newTodoTitle);
      userTodo.setTodoDesc(newTodoDesc);
      mUserTodoRef.child(userTodo.getTodoKey()).setValue(userTodo);
  }
  
  public void removeTodo(UserTodo userTodo){
      mUserTodoRef.child(userTodo.getTodoKey()).removeValue();
  }
  }
  </code></pre>
  
  <p>After the user clicked on Next button in <code>AddUser</code> activity, the user data is straightly added to Firebase, and the user will be redirected to <code>ToDo</code> page where the user can add to do items. How to pass the push key created in <code>AddUser</code>, so that when the user add the to do items, the items will be added under the user?</p>
  
  <p>Is using intent the right way?</p>
  
  <p>Please don't ask me why I need to let user add to do list right after the user is created. It's needed this way.</p>
  
  <p>Thanks</p>
  
  <p>Edit: I'm sorry I should mention that the intent should be passed to <code>UserTodoAdapter</code> class, so that in the Firebase database reference of <code>UserTodoAdapter</code>, I can point the reference to the key passed from <code>AddUser</code>.</p>
  
  <p>I have classes <code>UserDetails</code> and <code>UserTodo</code>, for activities <code>AddUser</code> and <code>ToDo</code> respectively to handle data in Firebase.</p>
  
  <p>Eventually the data will look like this:</p>
  
  <pre><code>{
    "users":{
      "push_id":{
          "userid":"123456",
          "username":"My User",
          "todo_s":{
              "push_id":{
                  "todo1":"Title1",
                  "todo_desc":"Description"
              },
              "push_id":{
                  "todo2":"Title2",
                  "todo_desc":"Description"
              },
          }
      },
    }
  }
  </code></pre>