-1

Here I have 3 activity and ActivityA contain textView which show username , ActivityB contain only button to redirect ActivityC, ActivityC Contain EditText and save button.

enter image description here

Now i want to change updated text from ActivityC to direct ActivityA when save button clicked without refreshing any Activity than what i have to do? can you please suggest simple way.

I have a same requirement in complex project, i need to save user location at server side using api call and show updated data in ActivityA.

Adil
  • 812
  • 1
  • 9
  • 29

3 Answers3

2

Another way would be to use LocalBroadcastManager. The code will be as follows.

//ACTIVITY A CODE

TextView username_tv;
String username;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    username_tv = (TextView) findViewById(R.id.username_tv);

    //Get your username and store it in username
    username = getYourUsername();

    username_tv.setText(username);

    LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(this);
    lbm.registerReceiver(receiver, new IntentFilter("USER_NAME_CHANGED_ACTION"));  
  }

  public BroadcastReceiver receiver = new BroadcastReceiver() {
  @Override
    public void onReceive(Context context, Intent intent) {
      if (intent != null) {
        username = intent.getStringExtra("username");
        username_tv.setText(username);            
      }
  }
};

//ACTIVITY C CODE

//ADD this code to your 'SAVE' Button Listener
Intent intent = new Intent("USER_NAME_CHANGED_ACTION");
intent.putExtra("username", editText.getText().toString());
// put your all data using put extra 
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);

For more information on localBroadcastManager look here.

udit7395
  • 626
  • 5
  • 16
0

I do not understand why you need this, but you can do it by saving the value of EditText in ActivityC in SparedPreferences here is an example how can make it. Then you should check in onResume() method of ActivityAif there is saved value in SparedPreferences and if has some get the value and set it to "Username" TextView.

MeLean
  • 3,092
  • 6
  • 29
  • 43
  • I have a same requirement in complex project, i need to save user location at server side using api call and show updated data in ActivityA. – Adil Feb 06 '18 at 05:20
0

EventBus will solve your problem http://greenrobot.org/eventbus/

Mitesh Machhoya
  • 404
  • 2
  • 8
  • event bus is good way but i dont know how to use of it can you please provide simple steps suggestion example, now i add this lib in my ptoject using this https://github.com/greenrobot/EventBus – Adil Feb 07 '18 at 05:08