0

I am stuck with a problem where I need to update the value of a adapter variable in another activity and coming back to first activity should also get that updated value.

my current flow is like. I am staring the BarDetailsActivity and passing the modal with intent from inside the adapter class like:

  Intent barDetailIntent = new Intent(getApplicationContext(), BarDetailActivity.class);
  barDetailIntent.putExtra("isfav", barsList.get(position));
  barDetailIntent.putParcelableArrayListExtra("barlist",barsList);
  mContext.startActivity(barDetailIntent);

Then on another activity I am getting that model from intent and changing its variable values as:

 gbar = in.getParcelableExtra("isfav");
 blist= in.getParcelableArrayListExtra("barlist");
 if (gbar.getmFavourite()) {    
    gbar.setmFavourite(false);
    } else {
   gbar.setmFavourite(true);
    }

Now on going back to my main Activity Value for "gbar.setmFavourite" is not updated on onresume of the MainActivity.

@Override
protected void onResume() {
    super.onResume();
    if(mAdapter != null){
        mAdapter.notifyDataSetChanged(); // here the adapter value is not updated.
    }
}

Please help me on this.

Harish Chauhan
  • 103
  • 1
  • 1
  • 9
  • when are you populating the adapter with your items? – malmling Feb 17 '17 at 08:03
  • are you sure, Is you adapter is not null here? – VikasGoyal Feb 17 '17 at 08:03
  • @Avi yes thats why I checked the adapter here but the gbar.setmFavourite(false); does'nt seems to work here. and the original instance is available there. – Harish Chauhan Feb 17 '17 at 10:19
  • You are passing a parcelable object and on another Activity you are updating its state. It will not change the state of the existing object in earlier activity. – VikasGoyal Feb 17 '17 at 10:47
  • You need to transfer changes via either Braodcast receiver or you can pass the changed object via EventBus. – VikasGoyal Feb 17 '17 at 10:48
  • Thanks Avi seems to be a valid reason. I would Like to do it in this way. – Harish Chauhan Feb 17 '17 at 11:13
  • Can you provide a link or sample code regarding this.? – Harish Chauhan Feb 17 '17 at 11:14
  • @HarishChauhan you should first understand that passing data via `Intent.putExtra()` does not carry the variable through to the next activity instead it does carry the values held in that variable that is why you would need to re-assign the variable to get the passed data. With that in mind you could use shared preference to keep and update the variable globally and so you can equally update your adapter. – landrykapela Feb 17 '17 at 15:11

1 Answers1

1

Use Event Bus to handle the problem.

  1. Register your First Activity to listen the Event and override onEvent method.
  2. Fire the stickyIntent with updated dataset from SecondActivity.
  3. In the onEvent method of the FirstActivity call notifyDataSetChanged with the updated dataset.
nnn
  • 980
  • 6
  • 13