Call onViewCreated in DashboardFragment
To let fragment's view lifecycle owner passed in to controllers.
For UI usage, view lifecycle owner is the best choice, controller could
use this lifecycle owner to observe events and do UI related works.
Usage DataSaverController as first example.
Bug: 287005021
Test: Manually for DataSaverController
Change-Id: Id965ea3a24d61f4d0ec6735632944f41f72ba06a
diff --git a/src/com/android/settings/applications/specialaccess/DataSaverController.kt b/src/com/android/settings/applications/specialaccess/DataSaverController.kt
index 3a2fdb0..baed0aa 100644
--- a/src/com/android/settings/applications/specialaccess/DataSaverController.kt
+++ b/src/com/android/settings/applications/specialaccess/DataSaverController.kt
@@ -51,7 +51,7 @@
preference = screen.findPreference(preferenceKey)!!
}
- fun init(viewLifecycleOwner: LifecycleOwner) {
+ override fun onViewCreated(viewLifecycleOwner: LifecycleOwner) {
viewLifecycleOwner.lifecycleScope.launch {
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) {
preference.summary = getUnrestrictedSummary(mContext)
diff --git a/src/com/android/settings/applications/specialaccess/SpecialAccessSettings.java b/src/com/android/settings/applications/specialaccess/SpecialAccessSettings.java
index 9f4c895..2cbc304 100644
--- a/src/com/android/settings/applications/specialaccess/SpecialAccessSettings.java
+++ b/src/com/android/settings/applications/specialaccess/SpecialAccessSettings.java
@@ -21,10 +21,6 @@
import android.app.settings.SettingsEnums;
import android.os.Bundle;
-import android.view.View;
-
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
import com.android.settings.R;
import com.android.settings.dashboard.DashboardFragment;
@@ -51,12 +47,6 @@
}
@Override
- public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
- super.onViewCreated(view, savedInstanceState);
- use(DataSaverController.class).init(getViewLifecycleOwner());
- }
-
- @Override
protected int getPreferenceScreenResId() {
return R.xml.special_access;
}
diff --git a/src/com/android/settings/dashboard/DashboardFragment.java b/src/com/android/settings/dashboard/DashboardFragment.java
index f8a5d76..d4acfa1 100644
--- a/src/com/android/settings/dashboard/DashboardFragment.java
+++ b/src/com/android/settings/dashboard/DashboardFragment.java
@@ -25,11 +25,14 @@
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
+import android.view.View;
import androidx.annotation.CallSuper;
+import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.VisibleForTesting;
import androidx.lifecycle.LifecycleObserver;
+import androidx.lifecycle.LifecycleOwner;
import androidx.preference.Preference;
import androidx.preference.PreferenceCategory;
import androidx.preference.PreferenceGroup;
@@ -170,6 +173,15 @@
}
@Override
+ public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
+ super.onViewCreated(view, savedInstanceState);
+ LifecycleOwner viewLifecycleOwner = getViewLifecycleOwner();
+ for (AbstractPreferenceController controller : mControllers) {
+ controller.onViewCreated(viewLifecycleOwner);
+ }
+ }
+
+ @Override
public void onCategoriesChanged(Set<String> categories) {
final String categoryKey = getCategoryKey();
final DashboardCategory dashboardCategory =
diff --git a/src/com/android/settings/datausage/DataSaverSummary.kt b/src/com/android/settings/datausage/DataSaverSummary.kt
index 13fbbfa..0828d36 100644
--- a/src/com/android/settings/datausage/DataSaverSummary.kt
+++ b/src/com/android/settings/datausage/DataSaverSummary.kt
@@ -19,11 +19,9 @@
import android.content.Context
import android.os.Bundle
import android.telephony.SubscriptionManager
-import android.view.View
import android.widget.Switch
import com.android.settings.R
import com.android.settings.SettingsActivity
-import com.android.settings.applications.specialaccess.DataSaverController
import com.android.settings.dashboard.DashboardFragment
import com.android.settings.search.BaseSearchIndexProvider
import com.android.settings.widget.SettingsMainSwitchBar
@@ -59,11 +57,6 @@
}
}
- override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
- super.onViewCreated(view, savedInstanceState)
- use(DataSaverController::class.java).init(viewLifecycleOwner)
- }
-
override fun onResume() {
super.onResume()
dataSaverBackend.addListener(dataSaverBackendListener)