android-lifecycle

When does a LifecycleRegistry instance start listening to LifecycleOwner's lifecycle changes?

南楼画角 提交于 2019-12-01 12:34:36
I've started learning architecture components, but can't find one thing. LifecycleFragment just creates a new LifecycleRegistry object, which does not start observing the fragment's lifecycle. I guess the LifecycleRegistry object starts listening to the fragment's lifecycle when we, for example, put it into LiveData.observe() as first param, but I haven't found any proof of this in source code. Question: When and how does a LifecycleRegistry object start to observe a fragment's lifecycle and refresh LifecycleRegistry.mState ? There is a ContentProvider called LifecycleRuntimeTrojanProvider

When does a LifecycleRegistry instance start listening to LifecycleOwner's lifecycle changes?

Deadly 提交于 2019-12-01 10:31:23
问题 I've started learning architecture components, but can't find one thing. LifecycleFragment just creates a new LifecycleRegistry object, which does not start observing the fragment's lifecycle. I guess the LifecycleRegistry object starts listening to the fragment's lifecycle when we, for example, put it into LiveData.observe() as first param, but I haven't found any proof of this in source code. Question: When and how does a LifecycleRegistry object start to observe a fragment's lifecycle and

How to execute code before Android app is killed

允我心安 提交于 2019-12-01 05:23:56
问题 I have an Android app that connects to surrounding devices currently running the same app , even if in background . To do this, I use WiF-Direct to advertise the fact that I am currently running said application. Therefore I need to stop advertising this as soon as the app is killed. onDestroy() cannot be used since it is not guaranteed to be called. onStop() and onPause() cannot be used since the app is still running. How can I achieve this? Currently the service is still being advertised

Is it mandatory to remove yourself as an observer from Android Lifecycle?

一个人想着一个人 提交于 2019-12-01 03:02:51
I am building an Android Java class which implements the LifecycleObserver interface. This is the constructor: public MyObserver(AppCompatActivity activity) { this.mActivity = new WeakReference<AppCompatActivity>(activity); activity.getLifecycle().addObserver(this); } Is it necessary to ever call removeObserver , using something like: @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void destroyListener() { if (this.mActivity.get() != null) { this.mActivity.get().getLifecycle().removeObserver(this); } } Or, can I observe forever? TL;DR: Nope. According to this link here , where a user

Is it mandatory to remove yourself as an observer from Android Lifecycle?

↘锁芯ラ 提交于 2019-11-30 22:41:24
问题 I am building an Android Java class which implements the LifecycleObserver interface. This is the constructor: public MyObserver(AppCompatActivity activity) { this.mActivity = new WeakReference<AppCompatActivity>(activity); activity.getLifecycle().addObserver(this); } Is it necessary to ever call removeObserver, using something like: @OnLifecycleEvent(Lifecycle.Event.ON_DESTROY) public void destroyListener() { if (this.mActivity.get() != null) { this.mActivity.get().getLifecycle()

Handle states of orientation changes

寵の児 提交于 2019-11-30 19:29:58
问题 How can I handle all states of orientation event occurs? Something like: before starting (save some screen states) when happening (animation purposes) after it's happen (load the screen state) I know that onConfigurationChanged can handle orientation changes. And I tried this: public void onConfigurationChanged(Configuration cfg) { saveState(); super.onConfigurationChanged(cfg); loadState(); } On saveState I store the lastIndex viewed on the Gallery on SharedPreferences. On loadState I get

DialogFragment displayed from onContextItemSelected doesn't survive onPause/onResume

一曲冷凌霜 提交于 2019-11-30 13:43:12
I have a DialogDragment which I can show one of two ways: 1) By tapping on a ListView item from its OnItemClickListener 2) By activating a the ListView's context menu and selecting a menu item Doing #1 works fine under all lifecycle events, but if I invoke it via #2 and I pause the activity (by going Home) and the resuming it via the task switcher, the dialog is no longer displayed. The fragment is there, and I can rotate the device and show the dialog. I experimented, and if I put the showing of the DialogFragment into a Handler with a delay of at least 1/2 seconds, it works. The following

Access to application class in Broadcast Receiver

我是研究僧i 提交于 2019-11-30 13:18:26
I want to check internet connection in Broadcast Receiver; And set result (A boolean flag) to a global variable, to use it on whole application, in if conditions; That if internet is disconnected, set a status imageview in main activity, to red image, and if connected, set it to green. I followed this topic. But there is no getApplication() in Broadcast Receiver; And iI should use getApplicationContext() instead. On another side, this topic: when writing code in a broadcast receiver, which is not a context but is given a context in its onReceive method, you can only call getApplicationContext(

onCreate() called while Activity is stopped (but not destroyed). Only after installation

半腔热情 提交于 2019-11-30 10:17:13
I'm working on an app that targets Api 19, and that is basically a Processing sketch. The problem I'm facing is that the first time my app is run, right after installing it, it works well until the user sends it to the background. Then, if they click on the app icon again, onCreate() is called but the activity is not destroyed or restarted. Some variables change and that produces strange behaviours. This happens ONLY the first time the app is used. After force closing it, this behaviour won't happen ever again (as far as I've tested). And this does not happen when launching the app from

Back and Nav Up have different results

南楼画角 提交于 2019-11-30 09:56:37
I'm having an issue with activity navigation and I can't figure out what I'm doing wrong. I have a MainActivity and a SettingsActivity but using the Back and Up (on the action bar) have two different results from the Settings Activity. For example If I press the Back button I get the following lifecycle callbacks within the MainActivity : V/lifeCycle: onOptionsItemSelected V/lifeCycle: onPause V/lifeCycle: onSaveInstanceState-Bundle[..] V/lifeCycle: onStop << PRESS BACK BUTTON >> V/lifeCycle: onRestart V/lifeCycle: onStart V/lifeCycle: onResume V/lifeCycle: onPostResume While if I press the