1

I want to create a quiz app. On ProfileActivity is the list name using recycler view. And on NewProfileActivity, user need to enter their name, and the name will be displayed on the ProfileActivity.

This is the code button in the NewProfileActivity:

final Button save_profile_button = findViewById(R.id.button_save);
    save_profile_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent save_profile_intent = new Intent();
            if (TextUtils.isEmpty(mProfileNameView.getText())) {
                setResult(RESULT_CANCELED, save_profile_intent);
            } else {
                String profile_name = mProfileNameView.getText().toString();
                save_profile_intent.putExtra(EXTRA_REPLY, profile_name);
                setResult(RESULT_OK, save_profile_intent);
            }
            finish();
        }
    });

It will go back to the ProfileActivity, and able to display the name user entered. But when I change the flow, from NewProfileActivity to QuestionActivity, the name does not appear on ProfileActivity. This the code that doesn't work:

final Button save_profile_button = findViewById(R.id.button_save);
    save_profile_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent save_profile_intent = new Intent();
            if (TextUtils.isEmpty(mProfileNameView.getText())) {
                setResult(RESULT_CANCELED, save_profile_intent);
            } else {
                String profile_name = mProfileNameView.getText().toString();
                save_profile_intent.putExtra(EXTRA_REPLY, profile_name);
                setResult(RESULT_OK, save_profile_intent);
            }

            Intent goto_question_intent = new Intent(NewProfileActivity.this, QuestionActivity.class);
            startActivity(goto_question_intent);
        }
    });

How can I change the flow and still able to display the name? Using this flow: MainActivity -> ProfileActivity -> NewProfileActivity -> QuestionActivity -> ResultActivity -> MainActivity -> ProfileActivity

  • You did not tell how you go back from QuestionActivity to ProfileActivity. – greenapps Jun 08 '18 at 08:28
  • `startActivity(goto_question_intent);` That should be `startActivityForResult(goto_question_intent);` too. And you should have put the data in that intent. The two sendResult() before it do nothing as you have seen. – greenapps Jun 08 '18 at 08:31
  • @greenapps thanks for the reply. I already put the activity flow. – Ruzain Irfan Jun 08 '18 at 08:37
  • Your problem is the same as describerd here https://stackoverflow.com/questions/50652343/start-activity-for-result-when-started-activity-is-not-the-returner/50655640#50655640 – greenapps Jun 08 '18 at 08:57

2 Answers2

0

Try using Shared Preferences for saving small amount of data.

https://developer.android.com/training/data-storage/shared-preferences

Once you save the data via Shared Preferences, you can get it and display it anytime you want regardless of Activity changes.

In your case, you can do something like this in your button click event

SharedPreferences pref = getApplicationContext().getSharedPreferences("YourPrefNameHere", Context.MODE_PRIVATE);
Editor editor = pref.edit();

editor.putString("your_key_name", "put the value you want to save here");

When you want to display the saved value just get it and diplay it in any Activity you want

pref.getString("your_key_name", null);
Tartar
  • 5,149
  • 16
  • 63
  • 104
0

If you just use once ,you can use startActivityForResult https://developer.android.com/reference/android/app/Activity#starting-activities-and-getting-results

  • Please, consider adding an extract of code and explaining why it can solve the asker's problem. See [how to answer](https://stackoverflow.com/help/how-to-answer). – Umbo Jun 08 '18 at 08:48