-1

I am currently working on a project for my programming class, it is a d&d character creator I've decided to do for my final project. I am nearly finished with it but have run into one problem. I want to take the user's data from a screen where they create a character and store it into a screen where they can view their created characters, but I am not sure how to do this. This is the code from the create a character screen:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_new_char_screen);

    Button btnSave = (Button)findViewById(R.id.btnSaveChar);
    btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (checkData())
                startActivity(new Intent(NewCharScreen.this, HomeScreen.class));
        }
    });

    Button btnEx = (Button)findViewById(R.id.btnExplanation);
    btnEx.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(NewCharScreen.this, ExplanationScreen.class));
        }
    });
}

private boolean checkData(){
    final EditText charName = (EditText) findViewById(R.id.txtNameInput);

    if (charName.getText().toString().isEmpty()){
        charName.setError("Enter character name");
        charName.requestFocus();
        return false;
    }

    return true;
}

}

If there is a need for the rest of my code or the project itself here is a link to a repository on GitHub: https://github.com/cbetlinski98/MAD105-final_project

3
  • You need to create a new Fragment/Activity and send the data using Bundle Commented Dec 9, 2018 at 22:44
  • 1
    Do you need to store it for later use as well or just for current app session? Commented Dec 9, 2018 at 22:52
  • Dimness, for the time being it's just a one time thing for the current app session, so every time you close the app the data will be deleted and have to be re-entered when you open the app again. Commented Dec 9, 2018 at 23:17

3 Answers 3

4

If you want to pass data from one activity to another one, you should pass it inside the Intent object, so intead of creating it inside StartActivity you can create it outside as a normal object, and use putExtra() to put data inside it.

Intent intent = new Intent(getBaseContext(), DestinationActivity.class);
intent.putExtra("OBJECT_PARAM_NAME", your_object);
startActivity(intent);

To recover data on the other activity you can use

String sessionId= getIntent().getStringExtra("OBJECT_PARAM_NAME");

Pass basic types

If you need to pass basic objects like int, float, strings, you can always pass them with putExtra() but to take them in the destination activity there are relative methods like getStringExtra() or getIntExtra() and many others.

Pass your class

If your user is a class defined by you, you can implement Serializable in that class, put it inside the intent with putExtra() and recover it from the other activity with getIntent().getSerializableExtra("OBJECT_PARAM_NAME") and then just cast it to your class before putting it inside an object

Sign up to request clarification or add additional context in comments.

Comments

2

In order to pass information across intents:

Intent i = new Intent(this, Character.class);
myIntent.putExtra("paramKey","paramValue1");
startActivity(i);

Then in your activity to obtain the parameters:

Intent i2 = getIntent();
 i2.getStringExtra("paramKey");

Comments

1
startActivity(new Intent(CurrentActivity.this, AnotherActivity.class).putExtra(KEY, DATA));

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.