1

How to reload all data when application becomes active or becomes foreground? Something like this on iOS:

[[NSNotificationCenter defaultCenter]addObserver:self
                                            selector:@selector(viewDidLoad)
                                            name:UIApplicationDidBecomeActiveNotification
                                            object:nil];
walli
  • 37
  • 6
  • http://www.javatpoint.com/images/androidimages/Android-Activity-Lifecycle.png –  Aug 29 '15 at 23:27
  • Do you mean how you can save and reload data between different instances of your app? You can use shared preferences: http://developer.android.com/reference/android/content/SharedPreferences.html – Zarwan Aug 29 '15 at 23:27
  • I meant when you minimize app and after some time you wake it up from multitasking – walli Aug 29 '15 at 23:31

1 Answers1

0

1) In Android you cannot yet clearly tell when an application as a whole is foregrounded. There have been some pretty good hacks though. Check out this Stack Overflow answer. Be careful however that you should use onStart and onStop rather than onResume and onPause like the answer specifies.

https://stackoverflow.com/a/15573121/2293095

2) You can tell though when a specific Activity (usually a single page in the application) is in the foreground. I assume by reloading data, you mean you want to reload data from the local database or get from the server. In either case you can do this by overriding the Activity's onResume() method like so.

@Override
public void onResume(){
    //generic function that will query or get from server
    reloadData(); 
    //make sure to refresh your view. Ex: ImageView
    imageView.invalidate();
}
Community
  • 1
  • 1
Tariq
  • 593
  • 2
  • 7
  • 21
  • I meant I want to reload MainActivity, when I maximize the app from multitasking – walli Aug 30 '15 at 00:14
  • 1
    I'm sorry, I think, I didn't understand it first. But now I do. Thanks for the advice :) – walli Aug 30 '15 at 00:34
  • Sure thing. Feel free to ask if you want me to clarify something. Also please mark this as the right answer if you feel it is :) – Tariq Aug 30 '15 at 17:17