1

This is my scenario

  • Activity A (List of user)
  • Activity B (User Add Activity)

Desired Flow in app

Step 1: A ==> B

Step 2: B ==> A

Step 3 (Required flow):

Activity A has to reload itself or call loadListView() function after Activity B exits or is closed.

onCreate(){
.......
....
        btnAddClient.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent( ActivityA.this, ActivityB.class);
                startActivity(intent);
            }
        });

        loadListView();
......
....
}
Aashis Shrestha
  • 342
  • 5
  • 13
  • 2
    it's difficult to say what the problem is, since you didn't say you had a problem. What you call "reload itself" is very generic. An activity that is stopped will eventually be destroyed and then recreated when you return so if you're calling `loadListView` from `onCreate` chances are it's being called. If not, you can move that to `onStart` but the truth is, this is all a "hack". You need to revisit your architecture. The activity shouldn't care whether it's being loaded or "reloaded". It should receive the data in the correct state and should not drive it. – Martin Marconcini Jun 25 '21 at 15:16
  • @MartinMarconcini Thanks and yes I was also looking for best practices regarding this app flow. Can you please suggest me some architecture related tutorials or links that you find helpful? – Aashis Shrestha Jun 27 '21 at 13:21
  • 1
    Well, you have [the official developer page](https://developer.android.com/), from there you have a few choices, [the developer guides](https://developer.android.com/guide) are probably a good starting point. After understanding [Android Fundamentals](https://developer.android.com/guide/components/fundamentals), I think taking a look at this specific architecture stuff should be done through [the Jetpack/Architecture documentation and samples](https://developer.android.com/topic/libraries/architecture) – Martin Marconcini Jun 28 '21 at 07:15

2 Answers2

2

Try to call loadListView() in onStart or onResume methods. It will refresh the list when you come back to your activity

Anatolii Chub
  • 1,228
  • 1
  • 4
  • 14
  • Great!! this also worked for me but is this a good practice? – Aashis Shrestha Jun 27 '21 at 13:31
  • Good practice to have some actual state of the app and subscribe for updates from this state. How you can implement it: Use a database. On the activity B you should store new item to the database. on the Activity A you should subscribe for updates from database in onCreate method. In this way you should not check changes in your list and reload list every time where activity re-started. Because updates will be received immediately. There are some mechanism that allows to subscribe for updates like LiveData or RxJava. Room database supports both approaches. – Anatolii Chub Jun 27 '21 at 15:08
  • 1
    Here you can find a detailed tutorial - how create small application using Room database library and LiveData: https://google-developer-training.github.io/android-developer-advanced-course-practicals/unit-6-working-with-architecture-components/lesson-14-room,-livedata,-viewmodel/14-1-a-room-livedata-viewmodel/14-1-a-room-livedata-viewmodel.html LiveData overview: https://developer.android.com/topic/libraries/architecture/livedata Room overview: https://developer.android.com/training/data-storage/room – Anatolii Chub Jun 27 '21 at 15:10
1

Getting a result from an activity

From your Activity A, call the Activity B using the startActivityForResult() method.

int RESULT_CODE = 123;
Intent intent = new Intent(this, ActivityB.class);
startActivityForResult(intent, RESULT_CODE);

Now in your ActivityA class, write the following code for the onActivityResult() method.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_CODE) {
        if(resultCode == Activity.RESULT_OK){
            bool status=data.getBooleanExtra("isUserAdded");
            if(status)
            {
             loadListView();
            }
        }
    }
} //onActivityResult

Return back to ActivityA with status.

Intent returnIntent = new Intent();
returnIntent.putExtra("isUserAdded",true);
setResult(Activity.RESULT_OK,returnIntent);
finish();
Bholendra Singh
  • 980
  • 7
  • 14
  • 1
    Great!! this worked for me but I found onActivityResult is deprecated. So for those who would like to update please refere https://stackoverflow.com/a/63654043/4370398 – Aashis Shrestha Jun 27 '21 at 13:29