Skip to content

Commit

Permalink
Settings: Fix inflate exception on search. Dynamically replace nested…
Browse files Browse the repository at this point in the history
… fragment.

   When PrivacyGuard is opened, the parent fragment would inflate the layout
   which contained a nested fragment. This is bad behavior. Since we couldn't
   keep track of the fragments lifecycle, the fragment we instantiated during
   inflation would cause an inflate exception if and when we toggled the search
   view within the current context.

   Mitigate the crash by programmatically replacing the fragment after instantiating it once.

   AndroidRuntime
          E  FATAL EXCEPTION: main
          E  Process: com.android.settings, PID: 12372
          E  android.view.InflateException: Binary XML file line MoKee#21: Error inflating class fragment
          E      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
          E      at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
          E      at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
           ...
          E  Caused by: java.lang.IllegalArgumentException: Binary XML file line MoKee#21: Duplicate id 0x7f1001a2, tag nul
             l, or parent id 0xffffffff with another fragment for com.android.settings.privacyguard.PrivacyGuardPrefs
          E      at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2120)
          E      at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:177)
          E      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
          E      ... 25 more

Change-Id: I6820ad7d35814f150eedf91140e21c0b8e23322b
  • Loading branch information
Adnan Begovic authored and scott committed Jan 31, 2015
1 parent 99d66e1 commit 423e578
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 19 deletions.
3 changes: 1 addition & 2 deletions res/layout/privacy_guard_manager.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
<FrameLayout
android:id="@+id/privacy_guard_prefs"
android:name="com.android.settings.privacyguard.PrivacyGuardPrefs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
Expand Down
22 changes: 11 additions & 11 deletions src/com/android/settings/privacyguard/PrivacyGuardManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package com.android.settings.privacyguard;

import android.app.FragmentTransaction;
import android.view.animation.AnimationUtils;
import android.app.Activity;
import android.app.AlertDialog;
Expand Down Expand Up @@ -85,6 +86,9 @@ public class PrivacyGuardManager extends Fragment
private final static String LAST_LIST_POS = "last_list_pos";
private final static String LAST_LIST_OFFSET = "last_list_offset";

// Privacy Guard Fragment
private final static String PRIVACY_GUARD_FRAGMENT_TAG = "privacy_guard_fragment";

// holder for package data passed into the adapter
public static final class AppInfo {
String title;
Expand All @@ -97,21 +101,17 @@ public static final class AppInfo {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

mActivity = getActivity();
mAppOps = (AppOpsManager)getActivity().getSystemService(Context.APP_OPS_SERVICE);

return inflater.inflate(R.layout.privacy_guard_manager, container, false);
}
View hostView = inflater.inflate(R.layout.privacy_guard_manager, container, false);

@Override
public void onDestroyView() {
super.onDestroyView();
FragmentManager fm = getFragmentManager();
Fragment f = fm.findFragmentById(R.id.privacy_guard_prefs);
if (f != null && !fm.isDestroyed()) {
fm.beginTransaction().remove(f).commit();
}
Fragment privacyGuardPrefs = PrivacyGuardPrefs.newInstance();
FragmentTransaction fragmentTransaction = getChildFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.privacy_guard_prefs, privacyGuardPrefs,
PRIVACY_GUARD_FRAGMENT_TAG);
fragmentTransaction.commit();
return hostView;
}

@Override
Expand Down
11 changes: 5 additions & 6 deletions src/com/android/settings/privacyguard/PrivacyGuardPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,18 @@

package com.android.settings.privacyguard;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.PreferenceScreen;
import android.preference.SwitchPreference;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.Utils;

public class PrivacyGuardPrefs extends SettingsPreferenceFragment implements
OnPreferenceChangeListener {
Expand All @@ -43,12 +38,16 @@ public class PrivacyGuardPrefs extends SettingsPreferenceFragment implements

private SwitchPreference mPrivacyGuardDefault;

public static PrivacyGuardPrefs newInstance() {
PrivacyGuardPrefs privacyGuardFragment = new PrivacyGuardPrefs();
return privacyGuardFragment;
}

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

addPreferencesFromResource(R.xml.privacy_guard_prefs);
PreferenceScreen prefSet = getPreferenceScreen();

mPrivacyGuardDefault = (SwitchPreference) findPreference(KEY_PRIVACY_GUARD_DEFAULT);
mPrivacyGuardDefault.setOnPreferenceChangeListener(this);
Expand Down

0 comments on commit 423e578

Please sign in to comment.