5

I have the activity A where I can navigate to A->B->C->D

In Activity D I have a button in which if user'll press this button, it'll go to Activity A. Here I'll create a new instance where user can update value here.

I finish the activity, it'll Go To Activity D,from Activity D user'll press Back Button
D->C->B->A
and will come to Activity A.

I need to show the updated value in Activity A, how can I update value in 'Activity A' ?

Manish Dubey
  • 4,206
  • 8
  • 36
  • 65
mohan
  • 13,035
  • 29
  • 108
  • 178

7 Answers7

4

You're going to want to have a whole bunch of startActivityForResult()s and basically chain them from D back to A.

See here: How can I pass values between Activities on Android?

Alternatively, you could store the value as a SharedPreferences and just look it up later, once you return to the original Activity.

Community
  • 1
  • 1
Michael
  • 3,334
  • 20
  • 27
4

I think you must use SharedPreference to hold data on your back button click or use Static variable.

// ------------------ To store data ------------------

SharedPreferences.Editor editor = sharedPreferences.edit();
                editor.putInt("key", countValue);
                editor.commit();

// --------------------- To get data ------------------

if ((keyCode == KeyEvent.KEYCODE_BACK)) {
    Intent home=new Intent(PersentActivity.this,DesiredActivity.class);

                 sharedPreferences = getSharedPreferences("MY_SHARED_PREF", MODE_PRIVATE);   
          int countValue = sharedPreferences.getInt("key", 0);

                startActivity(home);

                return true;
            }
2

Use Shared Preferences to save your data. Thus you will get the data in all your activities.

Renjith
  • 5,783
  • 9
  • 31
  • 42
1

if you are not storing the data while updating Activity A, I think better store them in static variables of Activity A itself and load the data in your onResume().

If you have a database then store the data into Activity A from the database in your onResume().

Anu
  • 1,884
  • 14
  • 30
0

Include this snippet in your D Activity.

@Override
 public void onBackPressed(){
     aMethodToUpdateYourActivityA();
    }
hshed
  • 657
  • 2
  • 8
  • 21
0

You can learn more about Activity lifecycle and managing data here Activity

As for your question you should do any updates to data from your onResume method inside your Activity A.

@Override
public void onResume(){
    //Update your data here
}

If your concern is HOW to pass data between them,you should probably use Shared preferences,as having a load of startActivityForResult on so many activities is not really advised. Also is it really necessary to have that gerarchy?It would be easier if as soon as the user updates data,you would simply startActivityA and pass data to it directly.

sokie
  • 1,966
  • 22
  • 37
0

This can be done in 2 ways:

  1. Use shared preferences and store your updated value, then onResume() of Activity A, read it from the same shared preferences and update the value.

  2. launch activities using startActivityForResult, and collect the values using intent object and pass it back to previous activities using setResult.

Michael
  • 3,334
  • 20
  • 27
agp
  • 76
  • 2