Stitches S
Stitches S

Reputation: 65

Calling the same method from different activities

I have searched for an answer to this issue and while there are many similar topics, none of the solutions suit my needs. I have this code in MainActivity and it works there but I need to call the same 'addPlayer' method in another activity.

I have also tried creating a new java class called 'AddPlayer.java' to call in both activities via buttons in each but I can't find a way to call it. Perhaps its in the way I've defined the .java class?

    public class AddPlayer extends AppCompatActivity {

    public void addPlayer (View view) {
       // followed by same content as method in MainActivity - edited to correct contexts

MainActivity code

public class MainActivity extends AppCompatActivity {

public void addPlayer (View view) {

    final ParseObject players = new ParseObject("Players");

    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    final AlertDialog.Builder builderInBuilder = new AlertDialog.Builder(this);

    final EditText nameInput = new EditText(this);
    nameInput.setInputType(InputType.TYPE_CLASS_TEXT);
    final EditText nickInput = new EditText(this);
    nickInput.setInputType(InputType.TYPE_CLASS_TEXT);

    builder.setTitle("New Player Details")
            .setMessage("Enter new player name...")
            .setIcon(android.R.drawable.gallery_thumb)
            .setView(nameInput)
            .setPositiveButton("Add Player", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    builderInBuilder.setTitle("New Player Details")
                            .setMessage("Enter player Nickname...")
                            .setIcon(android.R.drawable.gallery_thumb)
                            .setView(nickInput)
                            .setPositiveButton("Add Nickname", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    players.add("nickname", nickInput.getText().toString());
                                    players.add("username", nameInput.getText().toString());
                                    players.saveInBackground(new SaveCallback() {

                                        @Override
                                        public void done(ParseException e) {

                                            if (e != null) {

                                                Toast.makeText(MainActivity.this, "Player could not be added.\n" + e.getMessage(), Toast.LENGTH_SHORT).show();

                                            } else {

                                                Toast.makeText(MainActivity.this, "Player added successfully.", Toast.LENGTH_SHORT).show();

                                            }

                                        }
                                    });

                                }
                            })
                            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                                @Override
                                public void onClick(DialogInterface dialog, int which) {

                                    Toast.makeText(MainActivity.this, "Add New Player cancelled.", Toast.LENGTH_SHORT).show();
                                    dialog.cancel();

                                }
                            })
                            .show();

                }
            })
            .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                    Toast.makeText(MainActivity.this, "Add New Player cancelled.", Toast.LENGTH_SHORT).show();
                    dialog.cancel();

                }
            })
            .show();

}

I get no errors calling the method in MainActivity, I am just unsure of how best to call this method from 2 activities and the syntax required to call the separate java class - which has the correct package assignment.

I was expecting to be able to use

AddPlayer.addPlayer(); // or something similar

Perhaps the best way is just to have this code in both locations? Seems inefficient though...

Upvotes: 0

Views: 129

Answers (1)

Jimale Abdi
Jimale Abdi

Reputation: 2844

create class todo the task and call it

class AddPlayer{

public void player(Context ctx,View view){
   //do your work here
  }

}

then call that method like this

AddPlayer addp = new AddPlayer ();
addp.player(context,view);

I think the concept is like that, if you don't want to create a new instance to call the method, you can declare the method as static

Upvotes: 1

Related Questions