片段中的overridePendingTransition

时间:2016-08-20 14:40:35

标签: android android-fragments animation

我的主要活动使用带有viewpager和FragmentStatePagerAdapter的tablayout。我想用我的一个片段打开一个带有自定义动画的新活动,然后用动画再次关闭它。当我打开它时,我唯一看到的是黑屏。我就是这样做的:

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

我试图在不同的地方移动方法,但动画仍然不起作用,我只看到黑屏。如果我暂停并恢复到相同的活动,我会看到它。

3 个答案:

答案 0 :(得分:1)

查看CodingInFlow的解决方案: 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);
    }
}

答案 1 :(得分:0)

很抱歉,您的答案很晚,但是您需要在第二个活动中定义该活动的输入和输出方式。 例如

第一个片段

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

第二次活动

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

希望这可以解决您的问题

答案 2 :(得分:0)

非常简单地在您的片段中使用此代码

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