这似乎是关于这个问题的答案的音调,但都不完整。A有一个具有多种活动的应用程序。我所需要的只是一个IF
语句,这意味着如果我的应用程序进入一个背景,我会这样做.如果它进入前台我就这么做..。
!!!请记住,如果我从一个活动切换到另一个活动--这意味着我的第一个活动停止了,这并不意味着我的应用程序进入了后台--只是一个活动被另一个活动所取代。
发布于 2019-11-11 14:01:57
首先,在应用程序级梯度中添加一些必要的依赖项
dependencies {
def lifecycle_version = "2.1.0"
// ViewModel and LiveData
implementation "androidx.lifecycle:lifecycle-extensions:$lifecycle_version"
// alternatively - just ViewModel
implementation "androidx.lifecycle:lifecycle-viewmodel:$lifecycle_version" // For Kotlin use lifecycle-viewmodel-ktx
// alternatively - just LiveData
implementation "androidx.lifecycle:lifecycle-livedata:$lifecycle_version"
// alternatively - Lifecycles only (no ViewModel or LiveData). Some UI
// AndroidX libraries use this lightweight import for Lifecycle
implementation "androidx.lifecycle:lifecycle-runtime:$lifecycle_version"
annotationProcessor "androidx.lifecycle:lifecycle-compiler:$lifecycle_version" // For Kotlin use kapt instead of annotationProcessor
// alternately - if using Java8, use the following instead of lifecycle-compiler
implementation "androidx.lifecycle:lifecycle-common-java8:$lifecycle_version"
// optional - ReactiveStreams support for LiveData
implementation "androidx.lifecycle:lifecycle-reactivestreams:$lifecycle_version" // For Kotlin use lifecycle-reactivestreams-ktx
// optional - Test helpers for LiveData
testImplementation "androidx.arch.core:core-testing:$lifecycle_version"
}
您可以在这里引用最新版本的依赖项。
https://developer.android.com/jetpack/androidx/releases/lifecycle?authuser=1
您的应用程序必须扩展Application类,并覆盖下面示例中给出的以下方法。
您可以简单地使用arch库的生命周期感知组件实现相同的功能。我刚刚为您的用例发布了一个用于Kotlin和Java的示例代码。
Java版本。
public class YourApp extends Application implements LifecycleObserver {
@Override
public void onCreate() {
super.onCreate();
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);
}
// Application level lifecycle events
@OnLifecycleEvent(Lifecycle.Event.ON_START)
public void onEnteredForeground() {
//Timber.d("Application did enter foreground");
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
public void onEnteredBackground() {
// Timber.d("Application did enter background");
}
}
Kotlin版本:
class ZumeApp : Application(), LifecycleObserver {
override fun onCreate() {
super.onCreate()
ProcessLifecycleOwner.get().lifecycle.addObserver(this)
}
// Application level lifecycle events
@OnLifecycleEvent(Lifecycle.Event.ON_START)
fun onEnteredForeground() {
Timber.d("Application did enter foreground")
}
@OnLifecycleEvent(Lifecycle.Event.ON_STOP)
fun onEnteredBackground() {
Timber.d("Application did enter background")
}
}
我希望它能帮到你。如果它不起作用,或者在android文档中引用下面的文章,请留下评论。
https://developer.android.com/topic/libraries/architecture/lifecycle?authuser=1
发布于 2019-11-11 13:50:31
ProcessLifecycleOwner是有帮助的东西。它提到了It is useful for use cases where you would like to react on your app coming to the foreground or going to the background and you don't need a milliseconds accuracy in receiving lifecycle events.
看看事件Lifecycle.Event.ON_PAUSE
和Lifecycle.Event.ON_RESUME
发布于 2019-11-11 15:58:01
好的,它是通过在观察者中使用ProcessLifecycleOwner
来完成的。我的碎片上有这个。状态从ON_PAUSE变为ON_RESUME和back的唯一时间是整个应用程序进入后台并返回的时候。然后,取决于状态(在我的示例中),我要么使用背景方法获得我的位置,要么使用前台回调。
@Override
public View onCreateView(@NonNull LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
setRetainInstance(true);
//...
// some other stuff
//...
ProcessLifecycleOwner.get().getLifecycle().addObserver(new LifecycleEventObserver() {
@Override
public void onStateChanged(@NonNull LifecycleOwner source, @NonNull Lifecycle.Event event) {
if (event == Lifecycle.Event.ON_PAUSE) {
Log.e("Lifecycle", "Background");
getLocationFromBackground();
} else if (event == Lifecycle.Event.ON_RESUME) {
Log.e("Lifecycle", "Foreground");
getLocationFromForeground();
}
}
});
return view;
}
private void getLocationFromBackground() {
// some code
}
private void getLocationFromForeground() {
// some code
}
https://stackoverflow.com/questions/58808932
复制相似问题