我一直在尝试让我的Android应用程序能够在片段之间切换时保留您在片段中所做的操作。我希望能够在片段之间切换,而无需重新创建或替换它们,只需显示和隐藏它们。例如,如果您选中片段1中的一个框,但切换到片段2,然后返回片段1,我试着实现了这篇来自@Tester101 How can I switch between two fragments, without recreating the fragments each time?的文章中概述的解决方案
但是我不确定我是否让它正常工作。当我从HomeFragment切换到WidgetFragment时,WidgetFragment被放置在HomeFragment的顶部。
HomeFragment看起来像这样:x1c 0d1x但是当我切换到WidgetFragment时,它看起来像这样:
将WidgetFragment中的textview和复选框放在HomeFragment的顶部。
package loon.si.weather_app;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.fragment.app.FragmentManager;
import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;
import com.google.android.material.navigation.NavigationView;
import java.util.Objects;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawerLayout;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open_nav, R.string.close_nav);
drawerLayout.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container, new HomeFragment()).commit();
navigationView.setCheckedItem(R.id.nav_home);
}
}
@Override
public void onDestroy() {
super.onDestroy();
FragmentManager fragmentManager = getSupportFragmentManager();
if(fragmentManager.findFragmentById(R.id.nav_home) != null){
fragmentManager.beginTransaction().remove(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.nav_home))).commit();
}
if(fragmentManager.findFragmentById(R.id.nav_widget) != null){
fragmentManager.beginTransaction().remove(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.nav_widget))).commit();
}
}
@SuppressLint("NonConstantResourceId")
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
FragmentManager fragmentManager = getSupportFragmentManager();
switch (item.getItemId()){
case R.id.nav_home:
if(fragmentManager.findFragmentById(R.id.nav_home) != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.nav_home))).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.fragment_container, new HomeFragment(), "home").commit();
}
if(fragmentManager.findFragmentById(R.id.nav_widget) != null){
//if the other fragment is visible, hide it.
fragmentManager.beginTransaction().hide(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.nav_widget))).commit();
}
break;
case R.id.nav_widget:
if(fragmentManager.findFragmentById(R.id.nav_widget) != null) {
//if the fragment exists, show it.
fragmentManager.beginTransaction().show(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.nav_widget))).commit();
} else {
//if the fragment does not exist, add it to fragment manager.
fragmentManager.beginTransaction().add(R.id.fragment_container, new WidgetFragment(), "widget").commit();
}
if(fragmentManager.findFragmentById(R.id.nav_home) != null){
//if the other fragment is visible, hide it.
fragmentManager.beginTransaction().hide(Objects.requireNonNull(fragmentManager.findFragmentById(R.id.nav_home))).commit();
}
break;
}
drawerLayout.closeDrawer(GravityCompat.START);
return true;
}
@Override
public void onBackPressed() {
if (drawerLayout.isDrawerOpen(GravityCompat.START)){
drawerLayout.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
1条答案
按热度按时间zf2sa74q1#
1.为了在分片之间切换时保留分片的状态,需要使用FragmentTransaction类的addToBackStack方法,该方法将当前分片添加到回退栈中,用户可以通过返回按钮返回到上一个分片。
public void run();drawerLayout.closeDrawer(GravityCompat.START);
1.使用findFragmentByTag方法根据片段的标签名称而不是ID来查找片段,因为标签名称是唯一的,并且在隐藏或显示片段时不会更改。
if(fragmentManager.findFragmentByTag(“home”)!= null){transaction.show(Objects.requireNonNull(fragmentManager.findFragmentByTag(“home”)));} else { transaction.add(R.id.fragment_container,new HomeFragment(),“home”);} if(fragmentManager.findFragmentByTag(“widget”)!= null){ transaction.hide(Objects.requireNonNull(fragmentManager.findFragmentByTag(“widget”)));}断裂;