How to execute background task when Android app is closed / set to background?

The following code will help you to post your contents when your app is closed or in the background: import android.app.Service; import android.content.Intent; import android.os.Binder; import android.os.Handler; import android.os.IBinder; import android.widget.Toast; public class SendDataService extends Service { private final LocalBinder mBinder = new LocalBinder(); protected Handler handler; protected Toast mToast; public class LocalBinder extends Binder … Read more

Android Fragment onCreateView vs. onActivityCreated

If your view is static, then moving any code to the onActivityCreated method is not necessary. But when you – for instance, fill some lists from the adapter, then you should do it in the onActivityCreated method as well as restoring the view state when setRetainInstance used to do so. Also accessing the view hierarchy … Read more

Public static variables and Android activity life cycle management

If the process is killed then all static variables will be reinitialized to their default values. So whatever value you have set in Activity A will not persist Good explanation can be viewed here from 2:50 http://www.infoq.com/presentations/Android-Design Here are some instructions for those who want to test this issue manually: Create android v.4 emulator, then … Read more

View pager and fragment lifecycle

The FragmentPagerAdapter keeps additional fragments, besides the one shown, in resumed state. The solution is to implement a custom OnPageChangeListener and create a new method for when the fragment is shown. 1) Create LifecycleManager Interface The interface will have two methods and each ViewPager’s Fragment will implement it. These methods Are as follows: public interface … Read more

What is the difference between onPause() and onStop() of Android Activites?

No, if some activity comes into foreground, that doesn’t necessarily mean that the other activity is completely invisible. Consider the following case: Here we see both activities at the same time. The first activity with the fields is obscured by another activity, and the user can no longer interact with it. However, it is still … Read more

Android checking whether the app is closed

This answer uses ProcessLifecycleOwner to detect application visibility. which is a part of Android Architecture Component . 1. add this lib to your project implementation “android.arch.lifecycle:extensions:1.1.1” 2. Extend an application class that implements LifecycleObserver public class AppController extends Application implements LifecycleObserver { /////////////////////////////////////////////// @OnLifecycleEvent(Lifecycle.Event.ON_START) public void onEnterForeground() { Log.d(“AppController”, “Foreground”); isAppInBackground(false); } @OnLifecycleEvent(Lifecycle.Event.ON_STOP) public void … Read more

How to handle AsyncTask onPostExecute when paused to avoid IllegalStateException

If you need to synchronize your task with the activity lifecycle, I believe that Loaders are exactly what you need. More specifically, you should use AsyncTaskLoader to do the job. So now instead of running an AsyncTask, you launch your loader, then wait for response in a listener. If the activity is paused, you won’t … Read more