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);
Related
I want to change activity as soon as I get connected to A device on Bluetooth,While getting the data continuously from Bluetooth.
Link- https://github.com/googlesamples/android-BluetoothChat
There's not such thing as "change" activity.
To open an Activity:
Intent intent = new Intent(this, TargetActivity.class);
startActivity(intent);
To close an Activity:
finish();
If you want to make Activity look like a change rather than an open/close, you'll need to remove open/close animations in your styles.xml file:
<item name="android:windowAnimationStyle">#null</item>
When it comes to Theme Changing activity, now my solution is: when i pressed a theme option then recreate the whole activity and setTheme before super.oncreate().So the user will encounter screen splash when switch between themes. How to make it elegantly?
From an App called 'TickTick',it turns out to be very smooth during switch:
Start recent activity without transition may seems a bit better.
public void reload() {
Intent intent = getIntent();
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
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);
So I've got an activity in my app that is currently marked as
android:launchMode="singleTop"
...and I currently have logic in both onCreate and onNewIntent to make sure that the screen is always showing the data delivered by the newest Intent that launched. And I'd like to be able to change between Holo.Light and Holo.Dark based on the data delivered by that Intent.
Calling setTheme doesn't work (see these two links):
Why getApplicationContext().setTheme() in a Activity does not work?
http://code.google.com/p/android/issues/detail?id=4394
That second link has a workaround that involves creating a second AndroidManifest.xml entry that has the other theme and points to an empty subclass of the activity in question. This works, but it breaks singleTop (since there can now be two instances of the activity on the stack).
I'm out of ideas. Anybody know if there's any way to do this aside from rolling my own custom ActionBar view for this activity?
You need to set the theme using the setTheme() method, but then to reload the activity.
I have a singleTask activity, and a code that runs on API<11, so I have this code to reload the activity:
public void reload() {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}
I'm pretty much just finishing the activity and calling it again. I disable any transition animation to make the reloading look instant.
Since you're referring to the Holo themes I assume you're working with API 11+.
API 11 added the method Activity#recreate(), which sends your current activity through the same teardown/recreate process that normally happens for config changes such as rotating the screen between landscape and portrait orientation. Your onCreate method will be called again on a new Activity instance, allowing you to set the theme on the Activity before the window is initialized as usual.
The Google Books apps uses this tactic to switch between light/dark themes for "night mode."
I'll post my solution that doesn't really add nothing new here but merge the various tips together.
After changing the Theme of the activity and optionally ( depends on what you are looking for ) of the application:
public void updateTheme( Activity a, int themeID ) {
a.getApplication().setTheme( themeID );
a.setTheme( themeID );
}
You then have to recreate the activity ( just as after a configuration change ).
For the OS 11> there is a API , instead for previous versions you have to force it finishing and restarting the activity just as Udinic pointed out.
public boolean isBeforeHoneycomb() {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB;
}
public void reload() {
if( isBeforeHoneycomb() ) {
Intent intent = getIntent();
overridePendingTransition(0, 0);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
finish();
overridePendingTransition(0, 0);
startActivity(intent);
}else{
recreate();
}
}
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.