全屏DialogFragment解雇活动

时间:2016-09-21 15:13:32

标签: android android-layout android-fragments android-dialogfragment

我正在使用活动进行表格输入的全屏DialogFragment,将这两个问题作为主要来源: How to achieve a full-screen dialog as described in material guidelines?Android full screen dialog appears transparent and in the wrong position

每当我按下DialogFragment中的关闭按钮(向上按钮)时,它后面的活动也会关闭。第二个问题的答案解决了这个布局的其余部分,让印刷机通过,而不是工具栏。

fragment_dialog_add_edit.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:fitsSystemWindows="true"
    android:background="@color/md_white">

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay"/>

        </android.support.design.widget.AppBarLayout>

        <android.support.constraint.ConstraintLayout
            android:id="@+id/dialogLayout"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.design.widget.TextInputLayout
                android:id="@+id/textInputLayout2"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginStart="16dp"
                android:layout_marginLeft="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:layout_marginTop="8dp"
                app:layout_constraintTop_toTopOf="parent"
                android:hint="@string/hint_name">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/etxt_restaurant_name"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:textAppearance="@style/Base.TextAppearance.AppCompat.Display1"
                    android:inputType="text" />

            </android.support.design.widget.TextInputLayout>

            <android.support.design.widget.TextInputLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:id="@+id/textInputLayout"
                android:layout_marginEnd="16dp"
                app:layout_constraintRight_toRightOf="parent"
                android:layout_marginRight="16dp"
                android:layout_marginTop="16dp"
                app:layout_constraintTop_toBottomOf="@+id/textInputLayout2"
                android:layout_marginStart="16dp"
                app:layout_constraintLeft_toLeftOf="parent"
                android:layout_marginLeft="16dp"
                android:hint="@string/hint_location">

                <android.support.design.widget.TextInputEditText
                    android:id="@+id/etxt_restaurant_location"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:inputType="text" />
            </android.support.design.widget.TextInputLayout>
        </android.support.constraint.ConstraintLayout>
    </android.support.design.widget.CoordinatorLayout>
</android.support.v4.widget.DrawerLayout>

AddEditDialogFragment.java

package nl.verhoogenvansetten.restaurantrio;

import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.TextInputEditText;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;

public class AddEditDialogFragment extends DialogFragment {
    private static final String TAG = "AddEditDialogFragment";

    public AddEditDialogFragment() {}

    public static AddEditDialogFragment newInstance(String title) {
        AddEditDialogFragment frag = new AddEditDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        frag.setArguments(args);
        return frag;
    }

    public static AddEditDialogFragment newInstance(String title, String name, String location, byte[] image) {
        AddEditDialogFragment frag = new AddEditDialogFragment();
        Bundle args = new Bundle();
        args.putString("title", title);
        args.putString("name", name);
        args.putString("location", location);
        args.putByteArray("image", image);
        frag.setArguments(args);
        return frag;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dialog_add_edit, container, false);

        Bundle args = getArguments();

        Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar);
        String mTitle = getArguments().getString("title");
        toolbar.setTitle(mTitle);

        String mName;
        TextInputEditText mETxtName = (TextInputEditText) rootView.findViewById(R.id.etxt_restaurant_name);
        if ((mName = args.getString("name")) != null)
            mETxtName.setText(mName);

        String mLocation;
        TextInputEditText mETxtLocation = (TextInputEditText) rootView.findViewById(R.id.etxt_restaurant_location);
        if ((mLocation = args.getString("location")) != null)
            mETxtLocation.setText(mLocation);

        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);

        ActionBar actionBar = activity.getSupportActionBar();
        if (actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
            actionBar.setHomeAsUpIndicator(R.drawable.ic_close_white_24dp);
        }

        setHasOptionsMenu(true);
        return rootView;
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        Dialog dialog = super.onCreateDialog(savedInstanceState);
        dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
        return dialog;
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        menu.clear();
        getActivity().getMenuInflater().inflate(R.menu.menu_add_edit_dialog, menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();

        if (id == R.id.action_save) {
            // Save data to DB
            return true;
        } else if (id == android.R.id.home) {
            getActivity().onBackPressed();
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

为了打开Dialogs,创建了一个实用程序类:

DialogUtil.java

package nl.verhoogenvansetten.restaurantrio.util;


import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

import nl.verhoogenvansetten.restaurantrio.AddEditDialogFragment;

/**
 * Created by Bas on 21-9-2016.
 */

public class DialogUtil {
    public static void openAddDialog(FragmentManager fragmentManager, String title) {
        AddEditDialogFragment addFragment = AddEditDialogFragment.newInstance(title);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.add(android.R.id.content, addFragment, "add_edit_fragment").addToBackStack(null).commit();
    }

    public static void openEditDialog(FragmentManager fragmentManager, String title, String name, String location, byte[] image) {
        AddEditDialogFragment addFragment = AddEditDialogFragment.newInstance(title, name, location, image);
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
        transaction.add(android.R.id.content, addFragment, "add_edit_fragment").addToBackStack(null).commit();
    }
}

3 个答案:

答案 0 :(得分:0)

弹出片段onOptionsItemSelected

中的后台堆叠
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_save) {
        // Save data to DB
        return true;
    } else if (id == android.R.id.home) {
        // pop the back stack to remove this dialog
        FragmentManager manager = getFragmentManager();
        manager.popBackStack();
        manager.executePendingTransactions()
        return true;
    }

    return super.onOptionsItemSelected(item);
}

答案 1 :(得分:0)

不确定为什么要添加所有自定义逻辑来显示对话框片段 - 您应该只使用:

AddEditDialogFragment.newInstance("Title")
        .show(getSupportFragmentManager(), "add_edit_tag");

然后,您可以在按下主页按钮时拨打dismiss()

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_save) {
        // Save data to DB
        return true;
    } else if (id == android.R.id.home) {
        dismiss();
        return true;
    }

    return super.onOptionsItemSelected(item);
}

答案 2 :(得分:0)

就我而言,活动是通过其onBackPressed()方法关闭的。我添加了一个全局标志,表示全屏对话框已打开,并用它包装了活动的onBackPressed()操作:

public class MyActivity
    extends Activity {

  private boolean isOpened = false;

  public boolean setIsOpened(boolean isOpened) {        
    this.isOpened = isOpened;
  }

  @Override
  public void onBackPressed() {

    if (!isOpened) {
      // Activity's back press operations.
    }
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

    if (id == ru.asv.dokiks.R.id.viewMenuEdit) {        
      isOpened = true;    
    }
  }
}


public class MyFragment
    extends Fragment {

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {

  if (id == android.R.id.home) {

    ((Activity) getContext()).setIsOpened = false;

    dismiss();

    return true;
  }
}