android-lifecycle

onActivityCreated is deprecated, how to properly use LifecycleObserver?

久未见 提交于 2020-08-04 06:01:47
问题 Google deprecate fragment’s onActivityCreated() on Android and recommend to use LifeCycleObserver: To get a callback specifically when a Fragment activity's * {@link Activity#onCreate(Bundle)} is called, register a * {@link androidx.lifecycle.LifecycleObserver} on the Activity's * {@link Lifecycle} in {@link #onAttach(Context)}, removing it when it receives the * {@link Lifecycle.State#CREATED} callback. So I try to make it in recommended way, but only state I can observe in Logcat is just

Picasso: How to check if singleton is already set

允我心安 提交于 2020-07-09 07:41:11
问题 I am using Picasso for handling image loading in my app. In my Activity I do @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.loading_screen); setupPicasso(); // ... } private void setupPicasso() { Cache diskCache = new Cache(getDir(CacheConstants.DISK_CACHE_DIRECTORY, Context.MODE_PRIVATE), CacheConstants.DISK_CACHE_SIZE); OkHttpClient okHttpClient = new OkHttpClient(); okHttpClient.setCache(diskCache); Picasso picasso

How to catch lifecycle events with ProcessLifecycleOwner?

瘦欲@ 提交于 2020-07-09 04:03:44
问题 I am trying to (at least partially) determine when an application gets closed by the user to release some connections, etc. To do this, I am using the ProcessLifecycleOwner with my application class implementing LifecycleObserver . Despite taking the starting code from tutorials and other helpful articles, it does not seem to detect any lifecycle events. Most of the code came from this example. My application class: public class App extends Application implements LifecycleObserver { @Override

Is it possible that onPause() gets called if onResume() hasn't been called before?

蹲街弑〆低调 提交于 2020-06-17 07:57:10
问题 I register receiver in onResume() and unregister it in onPause() method. I can strange error in my Crashlytics reports: Fatal Exception: java.lang.RuntimeException Unable to pause activity {package-here}: java.lang.IllegalArgumentException: Receiver not registered: Caused by java.lang.IllegalArgumentException Receiver not registered: It happened 3 times (3 users) on 20k installs. Is it possible that onPause() gets called if onResume() hasn't been called before? I thought it isn't. 回答1: If it

Is it possible that onPause() gets called if onResume() hasn't been called before?

久未见 提交于 2020-06-17 07:57:08
问题 I register receiver in onResume() and unregister it in onPause() method. I can strange error in my Crashlytics reports: Fatal Exception: java.lang.RuntimeException Unable to pause activity {package-here}: java.lang.IllegalArgumentException: Receiver not registered: Caused by java.lang.IllegalArgumentException Receiver not registered: It happened 3 times (3 users) on 20k installs. Is it possible that onPause() gets called if onResume() hasn't been called before? I thought it isn't. 回答1: If it

Android live data - observe always fires after config change

拥有回忆 提交于 2020-05-25 15:28:44
问题 I'm currently refactoring my code to include ViewModel with LiveData provided by android.arch library. I have a simple activity that sends request for a password change to server and acts according to HTTP response code. For that purpose I have created class that extends ViewModel for data and a repository class to call server. My ViewModel class has one MutableLiveData field which I'm subscribing to from my activity using .observe(...) method. The issue is that code inside .observe(...)

Android live data - observe always fires after config change

怎甘沉沦 提交于 2020-05-25 15:26:51
问题 I'm currently refactoring my code to include ViewModel with LiveData provided by android.arch library. I have a simple activity that sends request for a password change to server and acts according to HTTP response code. For that purpose I have created class that extends ViewModel for data and a repository class to call server. My ViewModel class has one MutableLiveData field which I'm subscribing to from my activity using .observe(...) method. The issue is that code inside .observe(...)

Some fragment observers trigger after pop from back stack although data is not changed

让人想犯罪 __ 提交于 2020-05-21 07:25:47
问题 I have some problem in nested fragment in Kotlin. I have nested fragment with ViewModel. After resuming fragment from back button press all observers on viewModel LiveData triggers again although my data does not changed. First i googled and tried for define observer in filed variable and check if it is initialized then do not observer it again: lateinit var observer: Observer fun method(){ if (::observer.isInitialized) return observer = Observer{ ... } viewModel.x_live_data.observe