How to switch activity without animation in Android? - android

How can I use properly the Intent flag FLAG_ACTIVITY_NO_ANIMATION in AndroidManifest file? I supose my problem is trivial, but I can't find good example or solution to it.
<intent-filter>
<data android:name="android.content.Intent.FLAG_ACTIVITY_NO_ANIMATION" />
</intent-filter>
However no error is reported by compliator, but data isn't correct.
I just want to disable animation in case switching between activities. I can use getWindow().setWindowAnimations(0); in onCreate or onResume rather but using flag is better way, isn't it?
I can use also in code:
Intent intent = new Intent(v.getContext(), newactivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
getContext().startActivity(intent);
But I want to use this flag in Android Manifest. To disable animation also in case returning from second activity to first.

You can create a style,
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
and set it as theme for your activity in the manifest:
<activity android:name=".ui.ArticlesActivity" android:theme="#style/noAnimTheme">
</activity>
You can also define a style to specify custom entry and exit animations.
http://developer.android.com/reference/android/R.attr.html#windowEnterAnimation

If your context is an activity you can call overridePendingTransition:
Call immediately after one of the flavors of startActivity(Intent) or
finish to specify an explicit transition animation to perform next.
So, programmatically:
this.startActivity(new Intent(v.getContext(), newactivity.class));
this.overridePendingTransition(0, 0);

Try this code,
this.startActivity(new Intent(v.getContext(), newactivity.class).addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION));

You can also just do this in all the activities that you dont want to transition from:
#Override
public void onPause() {
super.onPause();
overridePendingTransition(0, 0);
}
I like this approach because you do not have to mess with the style of your activity.

The line in the theme style works fine, yet that replaces the animation with a white screen. Especially on a slower phone - it is really annoying.
So, if you want an instant transition - you could use this in the theme style:
<item name="android:windowAnimationStyle">#null</item>
<item name="android:windowDisablePreview">true</item>

Here is a one-liner solution that works for as low as minSdkVersion 14 which you should insert in you res/styles.xml:
<item name="android:windowAnimationStyle">#null</item>
like so:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
<item name="android:windowAnimationStyle">#null</item>
</style>
...
</resources>
Cheers!

This is not an example use or an explanation of how to use FLAG_ACTIVITY_NO_ANIMATION, however it does answer how to disable the Activity switching animation, as asked in the question title:
Android, how to disable the 'wipe' effect when starting a new activity?

After starting intent you can use this code :
Intent intent = new Intent(Activity1.this, Activity2.class);
overridePendingTransition(0, 0);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
startActivity(intent);
If used, intent will work with no animations or transitions

create your own style overriding android:Theme
<style name="noAnimationStyle" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
Then use it in manifest like this:
<activity android:name=".MainActivity"
android:theme="#style/noAnimationStyle">
</activity>

Related

Android: Black Screen between Activity

When I go one activity to another activity , between the transaction a Black screen is come for some seconds. I properly finish the activity before calling startActvity().
Am using android:theme="#android:style/Theme.Translucent" theme for my activity. Even though between the activity transaction a black screen is coming
Can any one please tell me how to resolve this
Thanks in advance :)
There is no need to finish activity before calling startActivity().
Make sure that you have set content view in the onCreate of called Activity and that you are not blocking UI thread (check onCreate, onStart and onResume if you have override them).
You don't need to manage finshing your activity, this will be managed automatically when the activity is no longer in view. Just use:
startActivity(new Intent(this, MyNextActivity.class));
And use this code in whatever method you are using to navigate the activity changes.
If you make sure your window is the background of your activities you can set the window background to a color other than black:
<item name="android:windowBackground">#drawable/window_background</item>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
<solid android:color="#color/window_background"/>
</shape>
windowBackground in Android 6 (Marshmallow)
The other option is to manage transitions, so there is no gap between the end of the first transition and the beginning of the second. However, you have not mentioned transitions.
How to remove the delay when opening an Activity with a DrawerLayout?
for disable this default animation create one style:
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
and set it as theme for your activity in the manifest:
<activity android:name=".ui.ArticlesActivity" android:theme="#style/noAnimTheme">
</activity>
Assumption :-
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.xyz);
// comment code here
}
If you go from activity A to B then try to comment code in OnCreate , OnResume in Activity B Like this and check what happen still black screen is coming or not.If coming then try to change theme.
If you have a finish() or FLAG_ACTIVITY_CLEAR_TASK - a blank screen may show up on pre ICS devices
To avoid this black screen you have to add one line in intent
overridePendingTransition (0, 0);
Example(kotlin):
val intent = Intent(applicationContext, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
startActivity(intent)
overridePendingTransition (0, 0)
Example(Java):
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
overridePendingTransition (0, 0);

White screen is displayed while switching between Activities

When I move from one Activity to another Activity, a white screen is displayed for 2 seconds. I am using this code:
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
How can I resolve this issue?
Create a Theme like this:
<style name="YourTheme" parent="YourParentTheme">
<item name="android:windowDisablePreview">true</item>
</style>
Apply this theme to your second activity
More detail in this link: http://www.tothenew.com/blog/disabling-the-preview-or-start-window-in-android/
If your activity contains more complex layouts, do not use finish() after setting flag. Use FLAG_ACTIVITY_CLEAR_TOP and _TASK instead and it will solve your problem.This worked for me perfectly
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.l̥FLAG_ACTIVITY_CLEAR_TOP );
startActivity(intent);
or use simply like below
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
While switching from ActivityOne to ActivityTwo, till ActivityTwo onCreate method gets executed default background is shown which is the white/black screen. Good practise is don't do heavy operation in onCreate. To fix the issue set transparent background to ActivityTwo as shown below.
<style name="YourTheme" parent="YourParentTheme">
<item name="android:windowBackground">#android:color/transparent</item>
</style>
In Manifest set above theme
<activity
android:name=".ActivityTwo"
android:theme="#style/YourTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Try adding intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); before calling startActivity(intent);
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
Try to add intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
If your activity contains more complex layouts/ contains large size background image it takes rendering, so only that white page is displaying. If you want to remove that time delay use low size png images and clear layout designs.
To go to next activity use flag
Intent intent = new Intent(this, SecondActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
use finish if you want to clear the activity means when you press back then there is no stack of activity.
So you want to clear then use finish otherwise don't use it.
By using FLAG_ACTIVITY_NEW_TASK you are getting white screen, remove this like use this. It will work.
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);

How to create custom PopupWindow from intent call?

I wanna create a custom PopupWindow similar to this :
http://android-er.blogspot.kr/2012/03/example-of-using-popupwindow.html
In this example, PopupWindow is created by a button click event from an activity, but i want to create PopupWindow via intent from another application.
Is it possible? Any comments will be very appreciated!
The idea is to declare a standard Activity to make it appear like a Popup window of sorts.
Use this code (standard boiler plate Intent code to trigger the Activity)
SOME_WIDGET.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getApplicationContext(), THE_POPUP_ACTIVITY.class);
startActivity(myIntent);
}
});
If, for example, you name the popup Activity as Popup, then replace the THE_POPUP_ACTIVITY.class with Popup.class
Now, for this Activity, in the Manifest, declare a theme. For example:
And this is the corresponding style declaration:
<style name="DialogNoTitleBar" parent="android:style/Theme.Dialog">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
Also, in the onCreate() of the Popup Activity, you might want to add this statement right after the setContentView(R.layout.THE_LAYOUT_XML); I say might because how you want it to appear may vary from how I program my popup Activity.
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Your requirement isn't really clear. Intents are usually used when you want to switch to another activity or maybe send the intent to a service of some sorts. If you want to open a dialog for an action, you don't really need intents.
Create an activity with your custom design, while registering the activity in the manifest file just add this android:theme="#android:style/Theme.Dialog"
and call the activity with your intent. Hope you got it. :)

Unable to add dialog when press my own softkeyboard key

I get the following android exception when I try to open a dialog. when i am press my own SoftKeyboard key how can I fix this problem?
BadTokenException: Unable to add window -- token null is not for an application
com.example.android.softkeyboard.SoftKeyboard.diqalog(SoftKeyboard.java:759)
com.example.android.softkeyboard.SoftKeyboard.onKey(SoftKeyboard.java:526)
android.inputmethodservice.KeyboardView.onModifiedTouchEvent(KeyboardView.java:1252)
First of all, you cannot present a dialog from a remote service, you can only do so from within a running Activity, that's why you're getting a BadTokenException. But there are solutions to this problem:
1) Present an Activity with Theme.Dialog theme:
<activity
android:name="com.srgtuszy.activity"
android:theme="#android:style/Theme.Dialog"
/>
And start the activity as a new task:
Intent intent = new Intent(context, MyActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
This way, you'll get an activity which will look just like a dialog.
2) Present an empty and transparent Activity and show an AlertDialog from within the activity
Declare and start the activity in manifest just as before, but use a transparent theme:
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">true</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
In the activity, override the onCreate() method and don's call setContentView() and present the AlertDialog:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Context context = this;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
dialog.setTitle("Hello!");
dialog.show();
}
This is a more hacky approach, but in this way you can show a dialog to the user without dismissing the input method, you can use to to present edit options for instance.
If you just want to notify the user about a certain event, consider using Notifications, they won't distract the user and pollute the UI.

Android - How to stop animation between activity changes

I have multiple different Activity in my app and I don't want any transition animation when changing between Activities. Below is the how I'm changing between Activities:
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
This works great the first time I start a new Activity. There is no animation, but when I go back to an Activity that is already started it seems like the "Intent.FLAG_ACTIVITY_NO_ANIMATION" is ignored and the default animation happens.
I can't seem to figure out why this is happening.
Have you tried overridePendingTransition()?
You can set FLAG_ACTIVITY_REORDER_TO_FRONT by code and FLAG_ACTIVITY_NO_ANIMATION in manifest as below:
Create noAnimTheme in res/values/styles.xml
<style name="noAnimTheme" parent="android:Theme">
<item name="android:windowAnimationStyle">#null</item>
</style>
or
<style name="noAnimTheme" parent="android:Theme.NoTitleBar">
<item name="android:windowAnimationStyle">#null</item>
</style>
and use it in manifest:
<activity android:name="SecondActivity" android:theme="#style/noAnimTheme"/>
I hope it helps
I wa needing this as I had to create activities on clicking the menus.
I did the following :
I added the FLAG_ACTIVITY_NO_ANIMATION flag to the intent. It stopped the animations while creating the activity for the first time.
However the activities in the stack which were called when we click on the same menu again (probably from a different activity), it had the animation.
So I added FLAG_ACTIVITY_NO_HISTORY to clear or rather finish the activity when it starts a new activity. This caused to create a new activity (without animation) when I click on the menu once again.
add this after creating the second intent
Intent i = new Intent(SecondActivity.this, FirstActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
i.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(i);
when you return to the first intent, animation is disabled, worked for me though
If you're using FLAG_ACTIVITY_REORDER_TO_FRONT then you can also override onNewIntent for later startActivity calls. This will just work for bring to front states instead of first call.
#Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
overridePendingTransition(R.anim.whatever, R.anim.whatever);
}
Sure, you must implement this in target activity.

Categories

Resources