0

I have a main activity where the username is displayed. When user wants to edit the username, it will go to a profile activity with codes:

Intent intent=new Intent(MainActivity.this, ProfileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("username", username);
startActivity(intent);

After the user edits the username (saved on server), he can press back key to return to the previous (main) activity. However, at this time, the username is not updated as there is no data transferred in.

How can I transfer the updated username when the user is returning from profile activity to main activity?

mrmoment
  • 727
  • 2
  • 10
  • 34

4 Answers4

2

You can start the activity as startActivityForResult(Intent i, int requestcode);

and in second activity

 Intent intent = new Intent();
                    intent.putExtra("key", yourvalue);
                    setResult(1, intent);
                    finish();

this will trigger onActivityResult your previous activity.

 protected void onActivityResult(int arg0, int arg1, Intent arg2) {
            //get the data from arg2

        }
Piyush
  • 1,973
  • 1
  • 19
  • 30
1

You need to start activity for Result. Follow steps to do so

Step1: Start activity with result

int REQUEST_CODE = 1;
startActivityForResult(intent, REQUEST_CODE);

Step2: return some value from ProfileActivity

Intent resultIntent = new Intent();
resultIntent.putExtra("NAME_OF_THE_PARAMETER", valueOfParameter);
...
setResult(Activity.RESULT_OK, resultIntent);
finish();

Step 3: collect data from the Main Activity

Overriding @onActivityResult(...).

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) { // Please, use a final int instead of hardcoded int value
    if (resultCode == RESULT_OK) {
        String value = (String) data.getExtras().getString("NAME_OF_THE_PARAMETER");

References

Community
  • 1
  • 1
Chintan Rathod
  • 25,864
  • 13
  • 83
  • 93
  • For step 2, as ProfileActivity returns to MainActivity by system call on back key, so I need to put the codes in some method like ProfileActivity's onDestroy()? – mrmoment Nov 11 '14 at 07:44
  • The ProfileActivity does not always need finish after user set his username. – mrmoment Nov 11 '14 at 07:54
  • I find I can remove "finish()" in Step 2 in my case. The result will still be returned to onActivityResult(). But a side effect is the resultCode is -1, not 1. – mrmoment Nov 11 '14 at 08:03
  • @mrmoment you can put this code inside any listener or you can also create a method for that. You need to call `finish()`. This will allow application to finish current running activity and return to previous one :) – Chintan Rathod Nov 11 '14 at 08:17
0

finish() your main activity first.

Intent intent=new Intent(MainActivity.this, ProfileActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("username", username);
startActivity(intent);
finish();     // Add this 

then after all the updates got over, recall your main activity, like this, From your ProfileActivity call MainActivity.

Intent intent=new Intent(ProfileActivity.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(intent);
finish();     // Add this 

Load Username from the server by default

Karthick pop
  • 616
  • 3
  • 16
  • Restarting the first activity will require repeated network communications from client and server for other parts, besides the username part. – mrmoment Nov 11 '14 at 07:59
0

You can Store your updated profile name inside Application class because Application class is created only once when we start app. You can easily Update your UI. Application class is only one in a single Android Project It's use for globally. I think this is helpful for you.

Ramkailash
  • 1,852
  • 1
  • 23
  • 19
  • The problem is how to trigger the update in the first activiy (MainActivity). The other answers mentioned I should use startActivityForResult(). But still thank you as I used to store data in shared_preference. – mrmoment Nov 11 '14 at 07:58