gpt4 book ai didi

android - 如何让一个Activitly正确的观察Lifecycle事件

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:47:51 24 4
gpt4 key购买 nike

目前,我需要执行一些操作,当

  • 应用程序启动。
  • 申请结束。
  • 但不是在 Activity 娱乐、配置更改期间......

因此,到目前为止,以下代码 fragment 对我来说非常有用。我从 CommonWare 中学到了这样的技巧 - https://commonsware.com/AndroidArch/previews/other-lifecycle-ownershttps://proandroiddev.com/react-to-app-foreground-and-background-events-with-processlifecycleowner-96278e5816fa

微笔记应用程序.java

public class WeNoteApplication extends Application {

public static class AppLifecycleObserver implements DefaultLifecycleObserver {
@Override
public void onResume(LifecycleOwner owner) {
// Do something when the application launched.
// But not during activity recreation, configuration change, ...
}

@Override
public void onPause(LifecycleOwner owner) {
// Do something when the application quit.
// But not during activity recreation, configuration change, ...
}
}

private static final AppLifecycleObserver appLifecycleObserver = new AppLifecycleObserver();

@Override
public void onCreate() {
super.onCreate();

initLifecycleObserver();
}

private void initLifecycleObserver() {
Lifecycle lifecycle = ProcessLifecycleOwner.get().getLifecycle();
lifecycle.removeObserver(appLifecycleObserver);
lifecycle.addObserver(appLifecycleObserver);
}
}

但是,我还需要通过使用 ActivityFragment 来执行一些操作,...例如,显示一个 DialogFragment

对于我的入口点主要 Activity,这是我尝试过的。

public class MainActivity extends AppCompatActivity implements DefaultLifecycleObserver {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

ProcessLifecycleOwner.get().getLifecycle().removeObserver(this);
ProcessLifecycleOwner.get().getLifecycle().addObserver(this);

setContentView(R.layout.activity_main);
}

@Override
public void onResume(LifecycleOwner owner) {
android.util.Log.i("CHEOK", "onResume LifecycleOwner called");
}

@Override
public void onPause(LifecycleOwner owner) {
android.util.Log.i("CHEOK", "onPause LifecycleOwner called");
}

@Override
public void onCreate(LifecycleOwner owner) {
android.util.Log.i("CHEOK", "onCreate LifecycleOwner called");
}
}

由于以下观察,它没有按预期工作

应用启动时

onCreate LifecycleOwner called
onResume LifecycleOwner called
onResume LifecycleOwner called <-- Why onResume of LifecycleOwner is called twice??

当我旋转设备时

onCreate LifecycleOwner called
onResume LifecycleOwner called <-- Why onCreate and onResume of LifecyclOwner is called during configuration change?

使用 LiveData 再试一次

我尝试使用 LiveData 以便 AppLifecycleObserverActivity 通信。但是,在配置更改期间,onResumeLiveData 将重新创建的 Activity 视为新的生命周期所有者。因此,它将再次触发它。

public class WeNoteApplication extends Application {

public MutableLiveData<LifecycleOwner> onResumeLiveData = new MutableLiveData<>();

public class AppLifecycleObserver implements DefaultLifecycleObserver {
@Override
public void onResume(LifecycleOwner owner) {
// This will only be called during app launch, not configuration change.
android.util.Log.i("CHEOK", "onResume callback happen in application");
onResumeLiveData.setValue(owner);
...


public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

WeNoteApplication.instance().onResumeLiveData.observe(this, new Observer<LifecycleOwner>() {
@Override
public void onChanged(@Nullable LifecycleOwner lifecycleOwner) {
// This will only be called during app launch
// This will also be called during configuration change.
android.util.Log.i("CHEOK", "onResume callback happen in activity");
}
});

所以,我有些困惑。 Activitly(或Fragment)观察Lifecycle事件的正确方法是什么?意思是,那些回调事件函数不应该被触发,在配置更改、 Activity 重新创建......

最佳答案

我在 Google 的“待办事项应用”项目中找到了它。您可以使用 SingleLiveEvent 而不是 MutableLiveData。希望对您有所帮助。

public class SingleLiveEvent<T> extends MutableLiveData<T> {

private static final String TAG = "SingleLiveEvent";

private final AtomicBoolean mPending = new AtomicBoolean(false);

@MainThread
public void observe(LifecycleOwner owner, final Observer<T> observer) {

if (hasActiveObservers()) {
Log.w(TAG, "Multiple observers registered but only one will be notified of changes.");
}

// Observe the internal MutableLiveData
super.observe(owner, new Observer<T>() {
@Override
public void onChanged(@Nullable T t) {
if (mPending.compareAndSet(true, false)) {
observer.onChanged(t);
}
}
});
}

@MainThread
public void setValue(@Nullable T t) {
mPending.set(true);
super.setValue(t);
}

/**
* Used for cases where T is Void, to make calls cleaner.
*/
@MainThread
public void call() {
setValue(null);
}
}

关于android - 如何让一个Activitly正确的观察Lifecycle事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51220611/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com