1

The current problem I am facing is getting values from my activity class. This is where im picking up my user input and passing it to my player to decide what should happen.

I am wondering if it makes sense to have a constructor in the activity class as well as the onCreate? I don't really understand the difference however I know that if I create a constructor in the class and set the value of the variable in the constructor it passes that variable. If I don't have a constructor the class is returning 0(null) to my Player class. I will include as little as possible and try to explain what I mean as I find it quite hard to explain without an example.

Game.java | Activity class

public class Game extends Activity implements SensorEventListener{

    private int yDirection, xDirection;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new GameView(this));
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                yDirection = 1;
                break;
            case MotionEvent.ACTION_UP:
                yDirection = 2;
                break;
        }
        return true;
    }

    public int getyDirection() {
        return yDirection;
    }

}

Player.java

public class Player {

    Game userInput;

    public Player(Context context){

        userInput = new Game();
    }
}

I havent included the part im calling it from however if I call the value from Game.java in the Player class here it will return 0(null) However if I log the variable name and view it from the Game.java class it will be the right value.

If I keep the player class the same but change the Game.java class as follows I will constantly get the value 5 returning as it is set in the constructor however it doesnt update as it should when called by the player class

Game.java | Activity class

public class Game extends Activity implements SensorEventListener{

    private int yDirection, xDirection;

    public Game(){
            yDirection = 5;
        }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(new GameView(this));
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {
        switch (motionEvent.getAction() & MotionEvent.ACTION_MASK) {
            case MotionEvent.ACTION_DOWN:
                yDirection = 1;
                break;
            case MotionEvent.ACTION_UP:
                yDirection = 2;
                break;
        }
        return true;
    }


    public int getyDirection() {
        return yDirection;
    }
}

All help is appreciated!

Frenchie
  • 151
  • 1
  • 1
  • 9
  • 3
    no, that makes no sense. You're not manually instantiating the activity. Do set up in onCreate – Tim Dec 20 '17 at 10:44
  • actually I see you *are* manually instantiating the activity - do not do this. Not just in this case - **never** do it – Tim Dec 20 '17 at 10:46
  • What do you mean @TimCastelijns, im reasonably new to programming and would like to get a firm understanding but im not there yet. – Frenchie Dec 20 '17 at 10:55
  • 1
    android system manages the lifecycle of certain components (e.g. activity). The system decides when an activity needs to be instantiated. You do not manage this yourself. Hence, you should not put constructors in activities and should not call `new Activity()` – Tim Dec 20 '17 at 10:56
  • Okay I understand, it's okay to `new Class()` though? How do you think I should access the getters in my Activity class? – Frenchie Dec 20 '17 at 10:58
  • 1
    yes, regular java classes you manage yourself. I think you should do it the other way around. Don't ask the activity what the value is, but send the value to the other class – Tim Dec 20 '17 at 10:59
  • Using setters rather than getters? – Frenchie Dec 20 '17 at 11:06
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/161614/discussion-between-frenchie-and-tim-castelijns). – Frenchie Dec 20 '17 at 11:13

1 Answers1

0

For passing values to an activity started by another you should use an Intent:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent);

and retrieve it this way:

String s = getIntent().getStringExtra("EXTRA_SESSION_ID");
  • 1
    please don't copy paste code from your own project that has no relevance to the question. You probably didn't notice, but there is only 1 activity here and it sure as hell isn't called SignoutActivity – Tim Dec 20 '17 at 10:48
  • He asks "The current problem I am facing is getting values from my activity class" as you may know, this solves that problem. Obviously, this is only an example. – Carles Arnal Castelló Dec 20 '17 at 10:53
  • this doesn't solve that problem at all, because the other class is not an activity – Tim Dec 20 '17 at 10:54
  • @TimCastelijns is right, I am trying to pass data from my activity to a seperate class for my Player(Player.java isnt assosiated to any activities) – Frenchie Dec 20 '17 at 10:56
  • In that case I would suggest here the use of the listener pattern between the activity and the game class. – Carles Arnal Castelló Dec 20 '17 at 11:00