Я после решения фрагмента предпочтение в этой странице https://developer.android.com/guide/topics/ui/settings.htmlДолжен ли я сделать новое действие для использования фрагмента настроек?
Вот код используется:
public static class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}
...
}
public class SettingsActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Display the fragment as the main content.
getFragmentManager().beginTransaction()
.replace(android.R.id.content, new SettingsFragment())
.commit();
}
}
Это решение работает, но прежде, чем я сделал это, я пытался что-то другое.
Сначала я попытался открыть настройки, не используя SettingsActivity. Я хотел, чтобы переключить мой основной фрагмент в моей основной деятельности с настройкой фрагментом как таковые (этот код в моем файл MainActivity):
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
fragmentTransaction.replace(R.id.container, new SettingsFragment());
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
Это R.id.container является FrameLayout в моем activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
tools:ignore="MergeRootFrame" >
</FrameLayout>
Этот подход, похоже, не работает, почему? Фрагмент настроек обходит текущий фрагмент, так как текущий фрагмент не заменяется.
Каковы были бы штрафы за производительность, если я захочу сделать новое действие с помощью фрагмента меню?
Благодарим за помощь.
Я пробовал это решение, но фрагмент настроек по-прежнему перекрывает мой основной фрагмент. – MrBuggy