2

I am working on a full-screen DialogFragment over an Activity for form input, using these two other questions as primary sources: How to achieve a full-screen dialog as described in material guidelines? and Android full screen dialog appears transparent and in the wrong position.

Whenever I press the Close button (Up button) in the DialogFragment, the Activity behind it is also closed. The second question's answer fixed this for the rest of the layout letting presses pass through, but not the Toolbar.

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

For opening the Dialogs, a utility class was created:

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();
    }
}
Community
  • 1
  • 1
DatBassie
  • 313
  • 5
  • 21

3 Answers3

0

Pop the backstack in your fragment's 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);
}
petey
  • 16,914
  • 6
  • 65
  • 97
0

Not sure why you've added all the custom logic to show your dialog fragment -- you should just use:

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

And then you can just call dismiss() when the home button is pressed:

@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);
}
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • 1
    It is strange but that ```dismiss``` is causing the acitivty to finish. Please see: http://stackoverflow.com/questions/40351042/dismiss-dialog-causes-activity-finish – JCarlosR Apr 07 '17 at 06:30
0

In my case activity was closed from its onBackPressed() method. I have added a global flag that fullscreen dialog is opened and wrapped activity's onBackPressed() operations with it:

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;
  }
}
Zon
  • 18,610
  • 7
  • 91
  • 99