riot
riot

Reputation: 556

How to keep a DialogFragment on top of newly created activities?

I have an Activity A, which opens a DialogFragment. In this dialog, a button opens an Activity B. I would like this Activity B to open below the DialogFragment (which remains open), and I don't want the dialog to be recreated.

How can I achieve this? Is there a way to change the DialogFragment's parent Activity?

Cheers.

Upvotes: 2

Views: 477

Answers (1)

SWAT
SWAT

Reputation: 1158

You can use a transition to do this.

public void Trans(View v){
    Intent intent = new Intent(this,SecondActivity.class);
    String transitionName = getString(R.string.transition_album_cover);
    ActivityOptionsCompat options =
            ActivityOptionsCompat.makeSceneTransitionAnimation(HomeActivity.this,
                    albumCoverImageView,   // The view which starts the transition
                    transitionName    // The transitionName of the view we’re transitioning to
            );
    ActivityCompat.startActivity(HomeActivity.this, intent, options.toBundle());
}

In the layout of HomeActivity:

    <ImageView
    android:layout_height="200dp"
    android:layout_width="200dp"
    android:src="@drawable/pic"
    android:id="@+id/transPic"
    android:onClick="Trans"
    android:transitionName="@string/transition_album_cover" />

And in the layout of SecondActivity

    <ImageView
    android:layout_height="300dp"
    android:layout_width="match_parent"
    android:scaleType="centerCrop"
    android:id="@+id/transPic"
    android:src="@drawable/pic"
    android:transitionName="@string/transition_album_cover" />

Here I used an ImageView as a common element in both HomeActivity and Second Activity. I believe you can try something of this sort for the dialogue box too. I have not done it for Dialog box myself though.

EDIT: Changing the parent of the dialogue is not something that is achievable this way. I didn't read that part when I was posting the answer. But this is still worth a try I guess, if you are not obliged to use a Dialog itself.

Upvotes: 1

Related Questions