16

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.

Nikola
  • 890
  • 1
  • 11
  • 35
  • If you comment out `overridePendingTransition`, same black screen is happened? – nshmura Aug 20 '16 at 15:32
  • no without it everything is as normal – Nikola Aug 20 '16 at 15:34
  • It is odd.. can you post animation_leave.xml and animation_enter.xml ? – nshmura Aug 20 '16 at 15:35
  • I changed them a few times with the exact same ones as the tutorials - the problem is not there, for what I know. And yes, it is very odd. The device I use is nexus 6 by the way. – Nikola Aug 20 '16 at 16:03
  • I think I can't reproduce this issue without your xml file. Will this issue be happened with pre-set animations like `android.R.anim.slide_in_left` and `android.R.anim.slide_out_right` ? – nshmura Aug 20 '16 at 16:08
  • lol. I changed it to android.R.anim.fade_in and android.R.anim.fade_out and it works. I will add the xml files in the question. – Nikola Aug 21 '16 at 10:52
  • The other xml files work as well, I am not sure how today the problem is fixed by itself. Must be android studio. Anyway, thanks for the help @nshmura – Nikola Aug 21 '16 at 11:01
  • You can refer this link https://stackoverflow.com/questions/6468349/how-to-remove-black-background-between-start-new-activity-during-slide-left-anim – Kinjal Dec 13 '17 at 05:35
  • I'm having a similar issue, I really hope someone can help me with it... Whenever I attempt to put ` overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left); ` after `startActivity(intent);`, `overridePendingTransaction` turns red and returns "cannot resolve method" error. Does anyone know why this might be happening? – AndroidDevBro Mar 18 '18 at 21:01

3 Answers3

3

very simple use this code in your fragment

getActivity().overridePendingTransition(R.anim.slide_up, R.anim.slide_down);
1

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);
    }
}
PeterPazmandi
  • 533
  • 10
  • 13
0

sorry for late answer but you need to define from the second activity that how it will translate in and out. e.g

In First Fragment

Intent intent = new Intent(getActivity(), My_Activity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_left);

In Second Activity

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

OR

@Override
    public boolean onSupportNavigateUp() {
        finish();
        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
        return false;
    }

Hope this can solve your problem

Khaliq Izrail
  • 89
  • 2
  • 10