7

I want to refresh fragments from an Actvitiy. (Let's call the activity A) Activity A contains the fragments.

When I finish Activity B then Activity A should refresh the fragments.

I was thinking this would be done in onResume? I tried simply restarting the activity through an intent but that's not usability friendly.

So how do I refresh fragments when I resume an activity?

user2883477
  • 153
  • 1
  • 3
  • 16
  • What do you mean by "refresh"? Do you mean you want to reload data, or do you mean you want to cause A to be redisplayed, or ? – Peri Hartman Dec 15 '13 at 18:45
  • … what do you mean by "refresh fragments"?! – Martin Marconcini Dec 15 '13 at 18:45
  • Basically I want to reload data. Because I want to 'restart' the fragments. So the OnCreateView in every fragment is called again. (thus reloading the data in there) – user2883477 Dec 15 '13 at 18:48
  • I would put the setup code in onStart(). The onCreate() and/or onCreateView() methods will just inflate the layout and wire up the widgets (the findViewById() stuff). – Rick Falck Dec 15 '13 at 22:17
  • 1
    I just figured out you can use OnStart in fragments... I am new to Android so yeah... Anyhow, thanks! – user2883477 Dec 16 '13 at 13:29

3 Answers3

14

Just put this code inside

onResume()

For example you want to update your listview when u return to your fregment.

@Override
public void onResume() {
    super.onResume();
    if (allowRefresh)
    {
        allowRefresh = false;
        lst_applist = db.load_apps();
        getFragmentManager().beginTransaction().detach(this).attach(this).commit();
    }
}

Here getFragmentManager().beginTransaction().detach(this).attach(this).commit();

This line detach and re-attach the fragment so your content will be updated.

Mehul
  • 2,141
  • 1
  • 15
  • 15
  • How do I do that from the activity? – Si8 Oct 20 '16 at 10:13
  • what can i write (this) here – Mahesh Gawhane Nov 30 '17 at 10:17
  • If you want to move this logic into the Activity, do it inside the [onPostResume()](https://developer.android.com/reference/kotlin/android/app/Activity#onpostresume) method to avoid the [Activity state lost error](https://www.androiddesignpatterns.com/2013/08/fragment-transaction-commit-state-loss.html) – lasec0203 Apr 15 '21 at 06:07
  • What's missing from this answer are the other locations where you would need to change the value of `allowRefresh`. In this case, in `onPause` it should be `true` and then in`onDestroyView` it should be `false` – lasec0203 Aug 16 '21 at 05:49
2

The onResume() get called always before the fragment is displayed. Even when the fragment is created for the first time . So you can simply move your from onViewCreated() to onResume.

@Override
public void onResume() {
super.onResume();

//move your code from onViewCreated() here 

}
Tien Duong
  • 2,517
  • 1
  • 10
  • 27
Greenkraftz
  • 154
  • 1
  • 4
0

I agree with Mehul But u can alse do it this way .if you want to replace your fragment you could just say

@Override
public void onResume() {
    super.onResume();
    if (allowRefresh)
    {
        allowRefresh = false;
        lst_applist = db.load_apps();
        getFragmentManager().beginTransaction().replace(Fragment_Container,fragment).commit();
    }
}
Sumanth Jois
  • 3,146
  • 4
  • 27
  • 42