处理父活动中的后退按钮

时间:2017-04-02 19:46:49

标签: android android-fragments

我的主要活动包含2个片段:

MainActivity

public class MainActivity extends AppCompatActivity implements FragmentOne.OnFragmentOneInteractionListener, FragmentTwo.OnFragmentTwoInteractionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 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 fragmentOne = new FragmentOne ();

            // Add the fragment to the 'fragment_container' FrameLayout
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.fragment_container, fragmentOne ).commit();


        }

    }

此活动最初显示片段一。

片段一个有一个监听的按钮,当它被点击时,它将用片段2替换它自己。单击该按钮时,片段1中的界面

//Main acitivty has this method which is a a method the interface in fragment one requires
@Override
    public void goToFragmentTwo() {

        //Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        //  setSupportActionBar(toolbar);

        // Create fragment and give it an argument specifying the article it should show
         FragmentTwo fragmentTwo = new FragmentTwo ();

        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // Replace whatever is in the fragment_container view with this fragment,
        // and add the transaction to the back stack so the user can navigate back
        transaction.replace(R.id.fragment_container, fragmentTwo );
        transaction.addToBackStack(FragmentTwo.BACKSTACK_TAG);

        // Commit the transaction
        transaction.commit();
    }

如果调用工具栏中的android后退按钮或后退按钮,则片段需要有一个确认对话框,询问您是否确定要离开。如果好,那么我有第二个活动,称为HomeActivity(此活动是MainActivity的父活动),它需要去,或者如果取消它只需要关闭对话框并保留在当前片段中。

所以在MainActivity中我覆盖了onBackPressed方法,它显示了一个:

@覆盖     public void onBackPressed(){         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) {
            MainActivity.super.onBackPressed();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
                finish();
        }
    });
    builder.show();
}

我遇到了这个代码的两个问题:

1)按工具栏中的后退按钮不会显示对话框 2)对话框的“确定”和“取消”功能无法正常工作。

基本上我想要完成的是我有一个HomeActivity,它有一个导航到MainActivity的按钮。当MainActivity被调用时,我将启动一个工作流,其中每个片段都是该工作流的一部分。如果按下后退按钮,则需要丢弃此工作流程,并且应将用户返回到HomeActivty。

2 个答案:

答案 0 :(得分:4)

我假设您正在使用ActionBar。

尝试在您的活动中添加此内容:

 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            onBackPressed();
            return true;
    }
    return false;
}

 @Override
public void onBackPressed() {
    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) {
            MainActivity.super.onBackPressed();
        }
    });
    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            finish();
        }
    });
    builder.show();
}

答案 1 :(得分:0)

1)你应该让你的工具栏后退按钮从你的活动中调用onBackPressed(),这样它们就可以共享相同的功能。

请参阅主要活动中的onSupportNavegationUp()。

2)让您的主要活动控制片段发生的事情,但如果必须,请让片段处理任何背压。

我希望这对你有用。

清单

<?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();
}