Home » Android » android – overridePendingTransition in a fragment

android – overridePendingTransition in a fragment

Posted by: admin May 14, 2020 Leave a comment

Questions:

My main activity uses tablayout with a viewpager and FragmentStatePagerAdapter. I want to open a new activity with a custom animation from one of my fragments and close it again with animation. When I open it, the only thing I see is a black screen. This is how I do it:

public class SearchActivity extends Fragment{
    ...
    Intent myIntent = new Intent(getContext(), DetailsActivity.class);
    startActivityForResult(myIntent, ACTIVITY_RESULT);
    getActivity().overridePendingTransition(R.anim.animation_enter,
                           R.anim.animation_leave);
    ...
}


public class DetailsActivity extends AppCompatActivity{
    ...
    Intent returnIntent = new Intent();
    returnIntent.putExtra("result", updatesPerformed);
    setResult(Activity.RESULT_OK, returnIntent);
    finish();
    overridePendingTransition(R.anim.animation_leave,
                           R.anim.animation_enter);
    ...
}

I tried to move the method in different places but the animation still doesn’t work and I see only black screen. If I pause and resume to the same activity I see it.

How to&Answers:

Check out CodingInFlow’s solution:
Slide Animation Between Activities

slide_in_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="-100%p"
    android:toXDelta="0" />

slide_out_left.xml

<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate
    android:duration="@android:integer/config_mediumAnimTime"
    android:fromXDelta="0"
    android:toXDelta="-100%p" />
</set>

MainActivity.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class MainActivity extends AppCompatActivity {

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

    public void openActivity2(View view) {
        Intent intent = new Intent(this, Activity2.class);
        startActivity(intent);
        overridePendingTransition(R.anim.slide_in_right, 
        R.anim.slide_out_left);
    }
}

activity_2.xml

<LinearLayout 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"
    android:gravity="center"
    android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This is Activity 2"
    android:textSize="30sp" />

</LinearLayout>

Activity2.java

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

public class Activity2 extends AppCompatActivity {

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

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_left,
            R.anim.slide_out_right);
    }
}