【问题标题】:How to stop animations between switching activities?如何在切换活动之间停止动画?
【发布时间】:2015-07-25 16:08:36
【问题描述】:

我正在开发一个安卓应用程序。应用程序中有两个活动。在操作栏中,我有一个按钮可以打开另一个活动。但是当我点击按钮时,默认的android动画会在切换活动之间发生。我尝试使用 intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);overridePendingTransition(0, 0); 但无济于事。这种方法不起作用。我的应用的最小 sdk 是 10。

我想要什么。

我希望当我点击按钮时不应该有动画。

我的代码

@Override
            public boolean onCreateOptionsMenu(Menu menu)
            {
                MenuInflater menuInflater = getMenuInflater();
                menuInflater.inflate(R.menu.main, menu);
                return true;
            }
            @Override
            public boolean onOptionsItemSelected(MenuItem item)
            {         
                switch (item.getItemId())
                {
                case R.id.action_button1:
                    Intent intent = new Intent(this, SecondActivity.class);
                    startActivity(intent);
                    finish();
                        overridePendingTransition(0, 0);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);

            }
                return true;

        }

【问题讨论】:

标签: android android-intent android-activity android-transitions


【解决方案1】:

您正在调用finish(),然后尝试通过向意图添加标志来覆盖动画。在startActivity() 中使用它之前,您必须将标志添加到意图中。试试这个而不是你的代码:

Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
overridePendingTransition(0,0); //0 for no animation
finish();

或者:

Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
overridePendingTransition(0,0); //0 for no animation

如果由于某种原因Inteng.FLAG_ACTIVITY_CLEAR_TOPfinish() 不适合您,请尝试在Manifest.xml 中为您的<activity> 设置android:noHistory=true。见here

【讨论】:

  • 它可以工作,但我希望在打开 SecondActivity 时关闭 FirstActivity。
  • 当我调用finish()时,动画再次出现,但是当我不调用finish()时,它工作正常。
  • @Faiyaz 试试intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  • {intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);} 不工作
  • @Faiyaz 以什么方式不起作用?尝试对清单文件进行更改的最新编辑。
【解决方案2】:

试试这个:)

protected static void start_Activity(Context context, Class<? extends Activity> activity) {
        if (context.getClass() == activity) { // current activity: do nothing
            return;
        }
        Intent intent = new Intent(context, activity);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);      
        ((Activity) context).finish();
        context.startActivity(intent);
        ((Activity) context).overridePendingTransition(0, 0);        
    }

build.gradle:

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "vn.com.abuabi.animation"
        minSdkVersion 10
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
}

主目录:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="vn.com.abuabi.animation" >

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".SecondActivity"
            android:label="@string/title_activity_second" >
        </activity>
    </application>

</manifest>

主活动:

package vn.com.abuabi.animation;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends ActionBarActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void doChange(View view) {
        Intent intent = new Intent(this, SecondActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);        
        finish();
        startActivity(intent);
        overridePendingTransition(0, 0);
    }
}

第二个活动:

package vn.com.abuabi.animation;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class SecondActivity extends ActionBarActivity {

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_second, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    public void doChange(View view) {
        Intent intent = new Intent(this, MainActivity.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        finish();
        startActivity(intent);
        overridePendingTransition(0, 0);
    }
}

2个布局文件:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="vn.com.abuabi.animation.SecondActivity">

    <TextView android:text="Second" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="doChange"/>

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView android:text="@string/hello_world" android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:onClick="doChange"/>

</RelativeLayout>

【讨论】:

  • 我已经在模拟器 API 16 中成功测试了我的应用程序(最小 sdk 16)
  • 其实我的app min sdk是10。
  • 您是否尝试过在其他模拟器(不同的 API)上运行?
  • 我在真实设备上运行它。一个是 android 2.3,另一个是 4.0.1,另一个是 4.1.2
  • 我已经构建了新应用程序(Min sdk 10),在模拟器 4.1(api 16)上测试,没有动画,不需要“noHistory”。我会告诉你我的新应用代码:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多