White screen is displayed while switching between Activities - android

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

Related

Preventing multiple async startActivity calls in Android

I have a listView with items that, when clicked, create an intent and start an activity with that intent using the startActivity method.
The issue is that items can be pressed multiple times while the asynchronous call to startActivity is made, which will open multiple activities one on top of each other.
Using the FLAG_ACTIVITY_SINGLE_TOP doesn't solve it either, perhaps because the activity being opened from intent 1 hasn't been placed at the top of the stack when intent 2 fires?
How can I handle this?
Manifest:
<activity
android:name=".Activity2"
android:parentActivityName=".Activity1"
android:launchMode="singleTask"/>
Intent code:
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.setFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
intent.putExtra("a", "b");
startActivityForResult(intent, REQUEST_CODE);
In Your Manifest file inside activity tag add android:launchMode="singleTask"
<activity
android:name="YourActivity"
android:launchMode="singleTask"/>
Solved with the FLAG_ACTIVITY_REORDER_TO_FRONT intent flag instead of FLAG_ACTIVITY_SINGLE_TOP

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

startActivity(intent) it doesn't working ? when my DemoActivity use android:launchMode="singleTask",

This is my AndroidManifest.xml
<activity android:name=".ui.activity.ERPWebContainerActivity"
android:launchMode="singleTask"
android:screenOrientation="portrait"
android:theme="#style/Theme.Eallcn.Fullscreen">
</activity>
When I step into a new Demo1Activity, I deal with some data, Step to Demo2activity from Demo1Activity.
when I in the Demo2activity activity. I use this method: startActivity() doesn't work.
Intent intent = new Intent(this, ERPWebContainerActivity.class);
intent.putExtra("waitUploadImageLists", waitUploadImageLists);
startActivity(intent);
finish();
You can use android:launchMode="singleInstance"which work almost same.
but still if your requirement is to use singleTask only then you can use that in this way:
android:launchMode="singleInstance"
Just remove the launchMode="singleTask" and set FLAG_ACTIVITY_CLEAR_TOP|FLAG_ACTIVITY_SINGLE_TOP flag whenever call an intent to your activity
You can override this method, you can get the Intent data!
why ? because the activity didn't finish, When i startActivity form Demo2activit, It does't go to onCreate method!
you can get the reslove way: you can see Android: lunchMode
enter link description here
Try in this order.
finish();
Intent intent = new Intent(/*intent parameters, but as I can't copy and paste it from a picture then... */);
startActivity(intent);

Android remove Activity from back stack

Okay so I'm kind of stumped on what to do with this. So I have the MainActivity, and from there an Activity can be launched to DegreePlanActivity, and from there another Activity can be launched to EditDegreePlan. I've got EditDegreePlan set to noHistory in the AndroidManifest. The problem is after they save the EditDegreePlan it launches an Activity to DegreePlan. So if the user presses Back they have to press it twice to get to MainActivity again. I want to get rid of that so they only have to press it once. I'm stumped on how to do this though.
If I set DegreePlanActivity to noHistory then they couldn't press Back to it while in EditDegreePlan.
I've tried overriding onBackPressed method and launching an intent to MainActivity. The problem then is that they have to press Back multiple times to exit the app then.
What should I do?
FLAG_ACTIVITY_CLEAR_TOP clears your Activity stack , you can use the code below:
Intent intent = new Intent(this, Activity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Remember that this flag clears just Intermediate Activities , for example if you have A,B,C in your Back Stack then going from C Activity to D with this flag this does not clear Back Stack and the Stack would be A,B,C,D but if you go from Activity D to Activity A with this flag , B,C,D Activities will pop up from the stack and you will have just A in the Back Stack .
To remove activity from back stack inside manifest add android:noHistory="true" to your activity inside the manifest file.
See sample below.
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.activity"
android:versionCode="1"
android:versionName="1.0">
<application android:name="MyApp" android:label="My Application">
<activity android:name=".LoginActivity"
android:noHistory="true"> //add this line to your activity inside manifest
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
simple solution for API >= 15 to API 23
user activity name in intent.
Intent nextScreen = new Intent(currentActivity.this, MainActivity.class);
nextScreen.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | IntentCompat.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(nextScreen);
ActivityCompat.finishAffinity(currentActivity.this);
I would suggest that you use startActivityForResult(), instead of simply startActivity(), when you launch the EditDegreePlan-Activity, as described in the Android tutorials.
In the EditDegreePlan-Activity you then call
setResult(RESULT_OK);
finish();
If you don't expect any data from the EditDegreePlan-Activity, then you don't necessarily have to implement the onActivityResult.
It seems, that you will get the desired behavior if you do not specify any flags at all. The back button would just do the right thing. To get an activity closed from within your code use the finish() method it has the same effect as the user pressing the back button. So you will automatically arrive at DegreePlan when you finish the EditDegreePlan, no need to call any Intents either.
You can call finish before you start your new activity. This will remove the current activity from the stack, so when you press back button from the next activity, the first activity will not be called from the stack history.
Intent i = new Intent(MainActivity.this, NextActivity.class);
finish();
startActivity(i);
Here is your flow:
MainActivity --> DegreePlanActivty --> EditDegreePlan--> DegreePlan --> MainActivty
Override these method inside your "DegreePlan"
public void onBackPressed() {
super.onBackPressed();
Intent goToMainActivity = new Intent(getApplicationContext(), MainActivity.class);
goToMainActivity.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); // Will clear out your activity history stack till now
startActivity(goToMainActivity);
}
use this to clear the stack :
menuIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
Intent intent = new Intent(getContext(), MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(intent);
You can add flags as follows and start Activity, try below code
Intent i = new Intent(activity, Payment.class);
i.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_TOP);
activity.startActivity(i);
This code should help you out: It is in Kotlin
private fun verifyIfUserIsLoggedIn(){
val uid = FirebaseAuth.getInstance().uid
if(uid== null){
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TASK.or(Intent.FLAG_ACTIVITY_NEW_TASK)
startActivity(intent)
}
}

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