我将BottomNavigationView添加到项目中,并针对BottomNavigationView中的三个图标实现了三个片段。我还使用了addToBackStack(),以便用户可以使用“后退”按钮导航回到早期的片段。现在的问题是,当我按下“后退”按钮时,它返回到先前的片段,但是相应的BottomNavigationView图标没有被激活,它保持了最后一次单击的图标的激活状态。
相关代码在下面共享。
MainActivity.java
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
int id = item.getItemId();
displaySelectedFragment(id);
return true;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
displaySelectedFragment(R.id.navigation_home);
}
private void displaySelectedFragment(int id){
Fragment fragment=null;
switch (id) {
case R.id.navigation_home:
fragment = new Home();
break;
case R.id.navigation_dashboard:
fragment = new DashBoard();
break;
case R.id.navigation_notifications:
fragment = new Notification();
break;
}
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_main, fragment);
ft.addToBackStack(null);
ft.commit();
}
}
@Override
public void onBackPressed() {
BackPressFragment fragment = ((BackPressFragment)
getSupportFragmentManager().findFragmentById(R.id.content_main));
if (fragment != null) {
fragment.onBackPressed();
}
}
}
家庭片段代码如下。
public class Home extends Fragment implements BackPressFragment{
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.home_fragment, container, false);
}
@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Are you sure you want to exit?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
getActivity().finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
类似地,下面给出了其他两个片段,分别命名为DashBoard和NotificationFragment。
/*----------------------------DashBoard Fragment-------------------------*/
public class DashBoard extends Fragment implements BackPressFragment {
public static DashBoard newInstance() {
return new DashBoard();
}
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.dash_board_fragment, container, false);
}
@Override
public void onBackPressed() {
if(getActivity().getSupportFragmentManager().getBackStackEntryCount()>0)
getActivity().getSupportFragmentManager().popBackStack();
}
}
/*--------------------------------------------------------------------*/
/*--------------------Notification Fragment---------------------------*/
public class Notification extends Fragment implements BackPressFragment{
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.notification_fragment, container, false);
}
@Override
public void onBackPressed() {
if(getActivity().getSupportFragmentManager().getBackStackEntryCount()>0)
getActivity().getSupportFragmentManager().popBackStack();
}
}
BackPressFragment.java接口
public interface BackPressFragment {
void onBackPressed();
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="@layout/content_main"/>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</android.support.constraint.ConstraintLayout>
请提出建议。