0

So There are 3 activities A, B and C. From Activity A, I go to activity B in activity B, I perform some tasks and I go to Activity C from Activity B.

I activity C, I have a button and some data, when I click the button I should update text in activity A. Is there a best way to do it?

edit: I do not want to go to back, when I click the button in activity C, but the text present in the activity a should be updated, later when I reach the activity a I should see the changes.

2 Answers2

1

I think, you should create a separate class that you will share between your activities A, B and C. The class should has a field, which will store text for your TextView in activity A. And there is a good variant to use Observer pattern (rxJava or custom decision).

Example: SharedStateModel is used to save state for ActivityA. That decision is not very clear, because it breaks dependency inversion rule so I recommend you to use Dagger 2 to inject SharedStateModel into ActivityA and ActivityC and manage its component's lifecycle like I describe in comments to SharedStateModeClass.

/*
Let's make that state-class a local singleton - you create it in onCreate of ActivityA and
further it can be destroyed in onDestroy of ActivityA.
 */
public class SharedStateModel {

    private PublishSubject<String> stateListener = PublishSubject.create();
    private String viewState;

    private static SharedStateModel instance;

    private SharedStateModel() {
    }

    public void setViewState(String viewState) {
        this.viewState = viewState;
        stateListener.onNext(viewState);
    }

    public PublishSubject<String> getStateListener() {
        return stateListener;
    }

    public void destroyViewState() {
        instance = null;
    }

    public static SharedStateModel getInstance() {
        if (instance != null) {
            return instance;
        } else {
            instance = new SharedStateModel();
            return instance;
        }
    }
}

public class ActivityA extends AppCompatActivity {

    @BindView(R.id.textView)
    private TextView textView;
    private SharedStateModel sharedStateModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        sharedStateModel = SharedStateModel.getInstance();
        sharedStateModel.getStateListener().subscribe(
                textViewState -> textView.setText(textViewState));
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        sharedStateModel.destroyViewState();
    }
}

 public class ActivityC extends AppCompatActivity {

    @BindView(R.id.button)
    private Button button;

    /*
    Let's assume that you want to send a message from EditText in ActivityC
     */
    @BindView(R.id.editText)
    private EditText editText;
    private SharedStateModel sharedStateModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ButterKnife.bind(this);
        sharedStateModel = SharedStateModel.getInstance();
        button.setOnClickListener(view -> sharedStateModel.setViewState(
                String.valueOf(editText.getText().toString())));
    }
}

And I want to recommend you good guides from Eugene Matsyuk if you want to learn Dagger 2.

0

2 way

  1. when you leaving activity C and going to activity A, pass the value from activity C to A just like you are going to a new activity. then set the text view.

  2. Pass your textView object from activity A activity B, then activity B to activity C. in activity C just setText normally with the textview object you bring from activity A to activity C.