1

I am developing an android app. There are two activities in the app. In the action bar, i have one button which opens the another activity. But when i click on the button the default android animation takes place between switching the activity. I tried using intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); and overridePendingTransition(0, 0); but to no avail. This method is not working. Min sdk of my app is 10.

What I want.

I want that there should be no animation when i click on the button.

My Code

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

        }
Faiyaz
  • 574
  • 1
  • 9
  • 39
  • 2
    possible duplicate of [switching activities without animation](http://stackoverflow.com/questions/6972295/switching-activities-without-animation) and [Android - How to stop animation between activity changes](https://stackoverflow.com/questions/5670754/android-how-to-stop-animation-between-activity-changes) – dhke Jul 25 '15 at 16:15
  • 1
    dhke its not duplicarte of that question. Solution given in that question is not working for me. – Faiyaz Jul 25 '15 at 16:17
  • Maybe better if you can post your code for others to have a better understanding. – Aruna Tebel Jul 25 '15 at 16:22
  • 1
    tibzon i added my code – Faiyaz Jul 25 '15 at 16:25

2 Answers2

3

You're calling finish() and then trying to override the animation by adding flags to the intent. You have to add the flag to the intent before you use it in startActivity(). Try this instead of your code:

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

Or:

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

If for some reason Inteng.FLAG_ACTIVITY_CLEAR_TOP or finish() aren't working for you, try to set android:noHistory=true for your <activity> in Manifest.xml. See here.

pez
  • 3,859
  • 12
  • 40
  • 72
  • 1
    Its working but i want the FirstActivity to be closed when SecondActivity is opened. – Faiyaz Jul 25 '15 at 16:34
  • 1
    When i call finish(), animation is again taking place but when i don't call finish(), its working fine. – Faiyaz Jul 25 '15 at 16:38
  • @Faiyaz Try `intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);` – pez Jul 25 '15 at 16:43
  • {intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);} not working – Faiyaz Jul 25 '15 at 16:45
  • @Faiyaz In what way isn't it working? Try the most recent edit involving changes to your manifest file. – pez Jul 25 '15 at 16:48
  • When i call finish(), animation is shown and when i dont call finish() animation is not shown. – Faiyaz Jul 25 '15 at 16:57
  • Its now working man. That android:nohHistory method actually worked. Thanks. Marking your answer as correct. – Faiyaz Jul 25 '15 at 17:02
0

Try this :)

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'
}

Mainifest:

<?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>

MainActivity:

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

SecondActivity:

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 layout files:

<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>
BNK
  • 23,994
  • 8
  • 77
  • 87