1) 您应该让您的工具栏后退按钮从您的活动中调用 onBackPressed(),以便它们共享相同的功能。
请参阅主活动中的 onSupportNavegationUp()。
2) 让您的 Main Activity 控制片段发生的情况,但如果必须,让片段处理任何后压。
希望对你有用。
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.johnurrutia.so_43172788">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.johnurrutia.so_43172788.MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolBar"
android:layout_width="0dp"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/fragment_container"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/toolBar"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:layout_editor_absoluteY="8dp">
</FrameLayout>
</android.support.constraint.ConstraintLayout>
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/btnGoFrag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Go To Fragment TWO" />
</LinearLayout>
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="@+id/goToFragmentOne"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Go To Fragment One" />
</LinearLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity
implements FragmentOne.OnFragmentOneInteractionListener, FragmentTwo.OnFragmentTwoInteractionListener {
public static final int FRAGMENT_ONE = 0;
public static final int FRAGMENT_TWO = 1;
private Toolbar toolbar;
private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
private ArrayList<Fragment> fragments = new ArrayList<>();
private int currentFragment = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.fragment_container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
fragmentOne = new FragmentOne();
fragmentTwo = new FragmentTwo();
fragments.add(FRAGMENT_ONE, fragmentOne);
fragments.add(FRAGMENT_TWO, fragmentTwo);
// Add the fragment to the 'fragment_container' FrameLayout
getSupportFragmentManager().beginTransaction()
.add(R.id.fragment_container, fragmentOne,"FRAGMENT_ONE")
.add(R.id.fragment_container, fragmentTwo, "FRAGMENT_TWO")
.hide(fragmentTwo)
.show(fragmentOne)
.commit();
}
}
@Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
@Override
public void onBackPressed() {
boolean fragmentProcessedTheEvent = ( (BackButtonListener) fragments.get(currentFragment)).onBackPressed();
if(!fragmentProcessedTheEvent) {
//The event is for Main Activity to exit
showActivityExitDialog();
}
}
public void showActivityExitDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Do you want to leave? ");
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
builder.show();
}
private void switchToFragment(int pos) {
if (currentFragment != pos) {
getSupportFragmentManager().beginTransaction()
.hide(fragments.get(currentFragment))
.show(fragments.get(pos))
.commit();
currentFragment = pos;
}
}
public void goToFragmentTwo(){
switchToFragment(FRAGMENT_TWO);
}
public void goToFragmentOne(){
switchToFragment(FRAGMENT_ONE);
}
}
FragmentOne.java
public class FragmentOne extends Fragment implements BackButtonListener{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
Button goToFragmentTwoBtn = (Button) v.findViewById(R.id.btnGoFrag2);
goToFragmentTwoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
( (OnFragmentOneInteractionListener) getActivity() ).goToFragmentTwo();
}
});
return v;
}
public interface OnFragmentOneInteractionListener{
public void goToFragmentTwo();
}
@Override
public boolean onBackPressed() {
/*
* if(fragment_has_any_use_for_back_press){
* process_it_here();
* return EVENT_PROCESSED;
* }
*
* */
return EVENT_IGNORED;
}
}
FragmentTwo.java
public class FragmentTwo extends Fragment implements BackButtonListener{
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_two, container, false);
Button goToFragmentTwoBtn = (Button) v.findViewById(R.id.goToFragmentOne);
goToFragmentTwoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
( (FragmentTwo.OnFragmentTwoInteractionListener) getActivity() ).goToFragmentOne();
}
});
return v;
}
@Override
public boolean onBackPressed() {
/*
* if(fragment_has_any_use_for_back_press){
* process_it_here();
* return EVENT_PROCESSED;
* }
*
* */
return EVENT_IGNORED;
}
public interface OnFragmentTwoInteractionListener{
public void goToFragmentOne();
}
}
BackButtonListener.java
public interface BackButtonListener {
public static final boolean EVENT_PROCESSED = true;
public static final boolean EVENT_IGNORED = false;
public boolean onBackPressed();
}