处理父活动中的后退按钮

use*_*127 2 android android-fragments

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

主要活动

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


        }

    }
Run Code Online (Sandbox Code Playgroud)

此活动最初显示片段一。

片段一有一个按钮,当点击它时,它会用片段二替换自己。当按钮被点击时,片段中的界面得到

//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();
    }
Run Code Online (Sandbox Code Playgroud)

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

因此,在 MainActivity 中,我覆盖了 onBackPressed 方法,该方法显示:

@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();
}
Run Code Online (Sandbox Code Playgroud)

我在使用此代码时遇到两个问题:

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

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

raf*_*007 5

我假设您正在使用 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();
}
Run Code Online (Sandbox Code Playgroud)