7

Ich habe eine SettingsActivity, die PreferenceActivity erweitert, um Einstellungen in meiner App zu zeigen.setDisplayHomeAsUpEnabled() funktioniert nicht in PreferenceActivity

Es gibt einen Zurückpfeil, aber es funktioniert nicht.

Hier SettingsActivity.java Code der Datei:

public class SettingsActivity extends PreferenceActivity implements SharedPreferences.OnSharedPreferenceChangeListener{ 

    private AppCompatDelegate mDelegate; 

    public static final String RESET_PASSWORD_KEY = "reset_password"; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     getDelegate().installViewFactory(); 
     getDelegate().onCreate(savedInstanceState); 
     super.onCreate(savedInstanceState); 
     addPreferencesFromResource(R.xml.preferences); 

     SharedPreferences sharedPreferencesCompat = PreferenceManager.getDefaultSharedPreferences(getBaseContext()); 

     Preference myPref = (Preference) findPreference(RESET_PASSWORD_KEY); 
     myPref.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { 
      public boolean onPreferenceClick(Preference preference) { 
       //open browser or intent here 
       Intent resetPasswordActivityIntent = new Intent(SettingsActivity.this, ResetPasswordActivity.class); 
       startActivity(resetPasswordActivityIntent); 
       return true; 
      } 
     }); 

    } 

    @Override 
    protected void onPostCreate(Bundle savedInstanceState) { 
     super.onPostCreate(savedInstanceState); 
     getDelegate().onPostCreate(savedInstanceState); 
    } 

    public ActionBar getSupportActionBar() { 
     return getDelegate().getSupportActionBar(); 
    } 

    public void setSupportActionBar(@Nullable Toolbar toolbar) { 
     getDelegate().setSupportActionBar(toolbar); 
     getDelegate().getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
    } 

    @Override 
    public MenuInflater getMenuInflater() { 
     return getDelegate().getMenuInflater(); 
    } 

    @Override 
    public void setContentView(@LayoutRes int layoutResID) { 
     getDelegate().setContentView(layoutResID); 
    } 

    @Override 
    public void setContentView(View view) { 
     getDelegate().setContentView(view); 
    } 

    @Override 
    public void setContentView(View view, ViewGroup.LayoutParams params) { 
     getDelegate().setContentView(view, params); 
    } 

    @Override 
    public void addContentView(View view, ViewGroup.LayoutParams params) { 
     getDelegate().addContentView(view, params); 
    } 

    @Override 
    protected void onPostResume() { 
     super.onPostResume(); 
     getDelegate().onPostResume(); 
    } 

    @Override 
    protected void onTitleChanged(CharSequence title, int color) { 
     super.onTitleChanged(title, color); 
     getDelegate().setTitle(title); 
    } 

    @Override 
    public void onConfigurationChanged(Configuration newConfig) { 
     super.onConfigurationChanged(newConfig); 
     getDelegate().onConfigurationChanged(newConfig); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     getDelegate().onStop(); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     getDelegate().onDestroy(); 
    } 

    public void invalidateOptionsMenu() { 
     getDelegate().invalidateOptionsMenu(); 
    } 

    private AppCompatDelegate getDelegate() { 
     if (mDelegate == null) { 
      mDelegate = AppCompatDelegate.create(this, null); 
     } 
     return mDelegate; 
    } 

    @Override 
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) { 
     if (key.equals(RESET_PASSWORD_KEY)) { 

     } 
    } 
} 

Hier preferences.xml Code der Datei:

<?xml version="1.0" encoding="utf-8"?> 
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"> 
    <Preference 
     android:title="xxx" 
     android:summary="xxxxxx" 
     android:key="reset_password" /> 
</PreferenceScreen> 

Hier ist, wie 'SettingsActivity' in AndroidManifest.xml definiert:

<activity 
    android:name=".SettingsActivity" 
    android:label="@string/title_activity_settings" 
    android:parentActivityName=".MainActivity"> 
</activity> 

Wie kann ich machen, dass Pfeil betriebsbereit?

Bitte lassen Sie es mich wissen.

Entschuldigung, wenn das Frageformat unangebracht erscheint. Ich bin immer noch ein Anfänger.

+0

wo ist Ihre Symbolleiste? – notsopopularguy

Antwort

22

Sie müssen rufen Sie Ihre private Methode setSupportActionBar() innerhalb onCreate() nennen.

Auch könnten Sie onOptionsItemSelected implementieren:

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    switch (item.getItemId()) { 
    case android.R.id.home: 
     onBackPressed(); 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 
+0

Schön, danke dafür – Sapher

2

Sie haben vergessen, diese Methode in Ihrem onCreate()

getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
+0

danke, aber das hat nicht geholfen! –

2

Ich habe gemacht, einige Annahmen, auf was Sie zu erreichen könnten versuchen. Dies sollte funktionieren, wenn Sie eine benutzerdefinierte Symbolleiste mit PreferenceActivity verwenden möchten. Der folgende Code geht in onPostCreateView()

LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent(); 
mToolBar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.toolbar, root,false); 
mToolBar.setTitle(R.string.title); 
// insert at top 
root.addView(mToolBar, 0); 

setSupportActionBar(mToolBar); 
getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
getSupportActionBar().setDisplayShowHomeEnabled(true); 
+0

wo sollte ich das hinzufügen? –

+0

@HammadNasir hat die Antwort aktualisiert – notsopopularguy