如何从 activity 调用片段函数?
How can I call a fragment function from an activity?
我想从 MainActivity 调用 SettingsFragment 中的 loadFromSharedPreferences() 函数。如果不将 ConstraintLayout 更改为 FragmentLayout,我该怎么做?请记住,我是 android 和片段的新手!
设置片段 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView6"
android:layout_width="113dp"
android:layout_height="49dp"
android:layout_gravity="top"
android:layout_marginStart="131dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="137dp"
android:text="@string/settings"
android:textSize="28sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/tvDarkSide"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvDarkSide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:text="@string/light_dark_mode"
android:textColor="#F10000"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/switchMode"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/switchMode" />
<Switch
android:id="@+id/switchMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="110dp"
android:layout_marginEnd="23dp"
android:text="Switch"
android:textSize="18sp"
android:checked="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SettingsFragmentclas:
public class SettingsFragment extends Fragment {
public SettingsFragment() {
// Required empty public constructor
}
public static final String SAVE_SWITCH = "saveSwitch";
public static final String IS_CHECKED = "isChecked";
Switch switchTheme;
SharedPreferences sharedPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_settings, container, false)
switchTheme = (Switch) view.findViewById(R.id.switchMode);
sharedPreferences = getActivity().getApplicationContext()
.getSharedPreferences( SAVE_SWITCH , Context.MODE_PRIVATE);
loadFromSharedPreferences();
switchTheme.setChecked(sharedPreferences.getBoolean(IS_CHECKED,false));
switchTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
SharedPreferences.Editor editor = sharedPreferences.edit();
//Scriere in fis SharedPreferences
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
editor.putBoolean(IS_CHECKED,true);
switchTheme.setChecked(true);
editor.apply();
((MainActivity) getActivity()).ToggleTheme(isChecked);
}
else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
editor.putBoolean(IS_CHECKED, false);
switchTheme.setChecked(false);
editor.apply();
((MainActivity) getActivity()).ToggleTheme(isChecked);
}
}
});
return view;
}
//Citire din fis SharedPreferences
private void loadFromSharedPreferences() {
boolean ischecked = sharedPreferences.getBoolean(IS_CHECKED, false);
switchTheme.setChecked(ischecked);
((MainActivity) getActivity()).ToggleTheme(ischecked);
}
}
MainActivity class:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//loadFromSharedPreferences()
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.AppThemeDark);
} else {
setTheme(R.style.AppTheme);
}
setContentView(R.layout.activity_main);
final DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
drawerLayout.openDrawer(GravityCompat.START);
}
});
NavigationView navigationView = findViewById(R.id.navigationView);
navigationView.setItemIconTintList(null);
NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
NavigationUI.setupWithNavController(navigationView, navController);
final TextView textTitle = findViewById(R.id.textTitle);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
textTitle.setText(destination.getLabel());
}
});
}
//AppCompatDelegate.setDefaultNightMode will cause your activities to reload automatically
public void ToggleTheme( boolean isChecked ){
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
从 activity 调用片段函数非常简单。
(你需要在 fragmet 的 xml 文件中添加一个 id)
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.your_fragment_id);
fragment.loadFromSharedPreferences();
FragmentManager manager = getSupportFragmentManager();
//Try to get instance of fragment from fragment Manager
Fragment fragment = manager.findFragmentById(R.id.your_fragment_id);
// create new instance of fragment if fragment not exist in fragment manager
if(fragment == null) {
fragment = new SettingsFragment();
}
fragment.loadFromSharedPreferences();
我想从 MainActivity 调用 SettingsFragment 中的 loadFromSharedPreferences() 函数。如果不将 ConstraintLayout 更改为 FragmentLayout,我该怎么做?请记住,我是 android 和片段的新手! 设置片段 XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".SettingsFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:id="@+id/textView6"
android:layout_width="113dp"
android:layout_height="49dp"
android:layout_gravity="top"
android:layout_marginStart="131dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="137dp"
android:text="@string/settings"
android:textSize="28sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/tvDarkSide"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/tvDarkSide"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:text="@string/light_dark_mode"
android:textColor="#F10000"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/switchMode"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/switchMode" />
<Switch
android:id="@+id/switchMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="110dp"
android:layout_marginEnd="23dp"
android:text="Switch"
android:textSize="18sp"
android:checked="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
SettingsFragmentclas:
public class SettingsFragment extends Fragment {
public SettingsFragment() {
// Required empty public constructor
}
public static final String SAVE_SWITCH = "saveSwitch";
public static final String IS_CHECKED = "isChecked";
Switch switchTheme;
SharedPreferences sharedPreferences;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_settings, container, false)
switchTheme = (Switch) view.findViewById(R.id.switchMode);
sharedPreferences = getActivity().getApplicationContext()
.getSharedPreferences( SAVE_SWITCH , Context.MODE_PRIVATE);
loadFromSharedPreferences();
switchTheme.setChecked(sharedPreferences.getBoolean(IS_CHECKED,false));
switchTheme.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
SharedPreferences.Editor editor = sharedPreferences.edit();
//Scriere in fis SharedPreferences
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
editor.putBoolean(IS_CHECKED,true);
switchTheme.setChecked(true);
editor.apply();
((MainActivity) getActivity()).ToggleTheme(isChecked);
}
else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
editor.putBoolean(IS_CHECKED, false);
switchTheme.setChecked(false);
editor.apply();
((MainActivity) getActivity()).ToggleTheme(isChecked);
}
}
});
return view;
}
//Citire din fis SharedPreferences
private void loadFromSharedPreferences() {
boolean ischecked = sharedPreferences.getBoolean(IS_CHECKED, false);
switchTheme.setChecked(ischecked);
((MainActivity) getActivity()).ToggleTheme(ischecked);
}
}
MainActivity class:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//loadFromSharedPreferences()
if (AppCompatDelegate.getDefaultNightMode() == AppCompatDelegate.MODE_NIGHT_YES) {
setTheme(R.style.AppThemeDark);
} else {
setTheme(R.style.AppTheme);
}
setContentView(R.layout.activity_main);
final DrawerLayout drawerLayout = findViewById(R.id.drawerLayout);
findViewById(R.id.imageMenu).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
drawerLayout.openDrawer(GravityCompat.START);
}
});
NavigationView navigationView = findViewById(R.id.navigationView);
navigationView.setItemIconTintList(null);
NavController navController = Navigation.findNavController(this, R.id.navHostFragment);
NavigationUI.setupWithNavController(navigationView, navController);
final TextView textTitle = findViewById(R.id.textTitle);
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
@Override
public void onDestinationChanged(@NonNull NavController controller, @NonNull NavDestination destination, @Nullable Bundle arguments) {
textTitle.setText(destination.getLabel());
}
});
}
//AppCompatDelegate.setDefaultNightMode will cause your activities to reload automatically
public void ToggleTheme( boolean isChecked ){
if (isChecked) {
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
}
else{
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
}
}
}
从 activity 调用片段函数非常简单。 (你需要在 fragmet 的 xml 文件中添加一个 id)
FragmentManager manager = getSupportFragmentManager();
Fragment fragment = manager.findFragmentById(R.id.your_fragment_id);
fragment.loadFromSharedPreferences();
FragmentManager manager = getSupportFragmentManager();
//Try to get instance of fragment from fragment Manager
Fragment fragment = manager.findFragmentById(R.id.your_fragment_id);
// create new instance of fragment if fragment not exist in fragment manager
if(fragment == null) {
fragment = new SettingsFragment();
}
fragment.loadFromSharedPreferences();