0

I am trying to create an 'auto-refresh' function in my application - so if one activity or fragment has been in the background for more than two minutes, it will automatically fetch new data from the api when entering the foreground.

However i have a case where Fragment A in Activity A, does the api call and shows the data in a list. When clicking a specific list element, you enter Activity B, where more information is shown.

Problem is - instead of making a api call for the specific listelement, when activity B enters the foreground after being in the background for at least two minutes - i'd rather just get the whole list (it's dynamic and usually pretty short) and ideally use the same code i use for the initial fetch in fragment A.

Is it possible to call a method from Activity B in Fragment A? How do i do it? And how do i pass the data back to Activity B afterwards, to update the UI there?

So i have a stack looking like:

Activity B (Foreground)

Fragment A in Activity A

And i want to call a method in Fragment A, from Activity B - and report back to Activity B, when the http request has responded.

Thanks in advance and please let me know if there is anything i need to elaborate.

Lars
  • 710
  • 5
  • 19
  • Possible duplicate of: https://stackoverflow.com/q/14439941/3991696 – vishwarajanand Jun 18 '18 at 10:32
  • @vishwarajanand It's not quite the same case - in the question mentioned he has an activity containing 5 fragments. I have two activities - one containing said fragment and one containing the list element view. I'll look into the question to see it there is anything i can use though. Thanks for your answer. – Lars Jun 18 '18 at 10:36

3 Answers3

0

You can use

@Override
public void onResume(){
    super.onResume();
    //Get value from api and update UI...
}
Thomas Ayoub
  • 29,063
  • 15
  • 95
  • 142
dariush
  • 216
  • 3
  • 10
  • And that's exactly what i do in most of activities. However in this exact case, i want to call a method in Fragment A, in Activity A, when Activity B resumes. – Lars Jun 18 '18 at 10:45
0

You could you try the following solution:

Write a public static method in fragment A in Activity A & call it from activity B => but it's important to handle the output of method for showing result while you are in activity B and for sharing data you can use SharedPreferences, local storage, or a static variable.

Lars
  • 710
  • 5
  • 19
  • A static method might be a solution - i just feel like there either should be some Android tool made for doing this - or that it's bad practice for some reason i can't figure out. Regarding the static method i feel like it isn't the best solution since i want to make sure the changes happens on the correct instance of Activity A/Fragment A, but i'll try to implement it and see if it works. Thanks for your answer. – Lars Jun 18 '18 at 11:00
0

There is no need to call method of Activity A->Fragment A from Activity B just to refresh data after two seconds. You can start a thread in onPause method of Fragment A or Activity A (as per your need).And later stop that thread in onResume.

//field
private Handler handler;
private Runnable runnable;

private void getDataFromNetwork(){
   handler = new Handler();
   runnable = new Runnable() {
     @Override
     public void run() {
        //Do something after 2000ms

       //call this method to again schedule this thread after 2000ms.
       handler.postDelayed(this, 2000);
   }
 };

   handler.postDelayed(runnable, 2000);
}

in onResume just remove the callbacks for this method.

handler.removeCallbacks(runnable);

But while using this approach always remember to remove the callbacks from the handler else it will become dangling thread and will cause memory leaks.

Avtar Guleria
  • 2,126
  • 3
  • 21
  • 33
  • 1
    It's not a bad idea. However i don't want to make the call after 2 minutes (and especially not after 2 seconds) - i want to make the call, when the activity goes into the foreground after it has been in the background for more than 2 minutes. The idea is that if it has been in the background for more than 2 minutes, it has probably been in the background for a while - and therefore it needs refreshing. Does it make sense? – Lars Jun 18 '18 at 11:47
  • so just replace it with 120000 for 2 minutes. And you will be calling thread in onPause of the ActivityA which will be called when the activity is in background – Avtar Guleria Jun 18 '18 at 11:54
  • oops, so you want to just refresh the Activity A if it was in background for more than 2 minutes right? In that case just save the System.currentTimeMillis() in onPause of ActivityA and in onResume of ActivityA compare the last saved time with the current time again and do the work – Avtar Guleria Jun 18 '18 at 11:58
  • Yes but the tricky part is i would also like to 're-use' the refresh logic from Activity A in Activity B. I think i've managed to make it work by creating another class though, will post my solution in here in a second when i've tested it. Thanks for the answers! – Lars Jun 19 '18 at 20:06