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

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

onActivityCreated is deprecated, how to properly use LifecycleObserver?

As per the changelog here The onActivityCreated() method is now deprecated. Code touching the fragment’s view should be done in onViewCreated() (which is called immediately before onActivityCreated()) and other initialization code should be in onCreate(). To receive a callback specifically when the activity’s onCreate() is complete, a LifeCycleObserver should be registered on the activity’s Lifecycle … Read more

Android lifecycle library: Cannot add the same observer with different lifecycles

In my case the problem was at lambda method of observer is empty. I just tried to add something to it and problem was solved. For example: gpsState.observe(this, (state) -> { Log.d(this.getClass().getSimpleName(), BaseNavigationActivity.this.toString()); }); Most likely that JVM define anonymous classes that use only static references and for such cases it become kinda singleton, so … Read more

What is lifecycle observer and how to use it correctly?

You can use ProcessLifecycleOwner to get your Application’s LifeCycle and to add a class as an observer of these events. You can implement LifecycleObserver in your Application Class: public class MyApplication extends MultiDexApplication implements LifecycleObserver @Override public void onCreate() { super.onCreate(); ProcessLifecycleOwner.get().getLifecycle().addObserver(this); } // Add these Lifecycle methods to observe when your app goes into … Read more

Android how to stop refreshing Fragments on tab change

By default, ViewPager recreates the fragments when you swipe the page. To prevent this, you can try one of three things: 1. In the onCreate() of your fragments, call setRetainInstance(true). 2. If the number of fragments is fixed & relatively small, then in your onCreate() add the following code: mViewPager = (ViewPager)findViewById(R.id.pager); mViewPager.setOffscreenPageLimit(limit); /* limit … Read more

NavUtils.navigateUpTo() does not start any Activity

My solution to OPs problem: public void navigateUp() { final Intent upIntent = NavUtils.getParentActivityIntent(this); if (NavUtils.shouldUpRecreateTask(this, upIntent) || isTaskRoot()) { Log.v(logTag, “Recreate back stack”); TaskStackBuilder.create(this).addNextIntentWithParentStack(upIntent).startActivities(); } else { NavUtils.navigateUpTo(this, upIntent); } } isTaskRoot() will return true if DeepLinkActivity is the root of a task (initial launch of application or application was previously terminated through task … Read more

: Can’t access the Fragment View’s LifecycleOwner when getView() is null i.e., before onCreateView() or after onDestroyView()

This can be fixed, by triggering the logic which is raising the error, from around end of onCreateView(…) callback (not onCreate(…) nor onAttach(…)). In getViewLifecycleOwner() documentation, I don’t think, I can explain better: The first method where it is safe to access the view lifecycle is onCreateView(LayoutInflater, ViewGroup, Bundle) under the condition that you must … Read more

Minimal android foreground service killed on high-end phone

A service started by startForeground belongs to the second most important group visible process: A visible process is doing work that the user is currently aware of, so killing it would have a noticeable negative impact on the user experience. A process is considered visible in the following conditions: It is running an Activity that … Read more