On clicking on android app icon, app takes lot of time to open and then white screen appears before launching MainActivity.
splash_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#drawable/splash_screen"
android:gravity="center" />
</layer-list>
styles.xml
<style name="LauncherLogoTheme" theme="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/sdp_launcher_background</item>
</style>
manifest file
<activity
android:name=".SplashActivity"
android:configChanges="orientation|keyboardHidden"
android:noHistory="true"
android:theme="#style/LauncherLogoTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
SplashScreenActivity
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
}
Please help on how to use splash screen in a correct way
If you have seen some apps using a splash screen, you must have noticed that they stay on the splash screen for 1 or half a second. Which is a standard for a splash screen. Mostly splash screen is white and an image generally with app icon/logo and app name.
Coming to your issue; the issue with your code is that you start another activity when the current activity is just created and just after that you are finishing it. I also don't see setContentView().
The recommended approach is to use a Handler and use a delayed callback to start the next activity and finish the current one.
public class SplashActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler(Looper.myLooper()).postDelayed(new Runnable() {
#Override
public void run() {
startActivity(new Intent(SplashActivity.this, MainActivity.class));
finish();
}
},1000);
}
}
Delayed starting of next activity and finishing of the current for 1 Second(1000 ms). Which will allow the Splash activity to be created and then resumed(become visible to user) smoothly before it is finished.
Related
I want to create app in which splash screen came only one time.
like i just downloaded the application from playstore and when i pressed on application icon. only very first time splash screen showed. for rest of the time. it don't open like go straight way on second activity. How can i do this.
thank you.
You can achieve this by creating a launcher activity without an UI and SharedPreferences:
public class LauncherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = getSharedPreferences("App", Context.MODE_PRIVATE);
// Get value from shared preferences,
// if null (app is run first time) the default value (second argument) is returned
boolean isFIrstRun = preferences.getBoolean("isFirstRun", true);
if (isFIrstRun) {
// set isFirstRun to false
preferences.edit().putBoolean("isFirstRun", false).apply();
// launch splash screen activity
} else {
// Launch other activity
}
}
}
And make sure LauncherActivity is set as the launcher activity in the app manifest.xml:
<activity android:name=".LauncherActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
This question already has answers here:
Android - Prevent white screen at startup
(18 answers)
Closed 4 years ago.
I created a splash screen for my application using an empty activity that stays visible for 3 seconds with a background image.
Usually, the application starts with a white screen before the background image becomes visible, however, some applications are already started with the "real" splash screen image.
How to implement this?
you can use splash screen in this way.
Add style for splash screen activity in styles.xml
<style name="SplashTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">#drawable/yourImageName</item>
</style>
Add that style as theme to your SplashScreenActivity in manifest file
<activity android:name=".SplashScreenActivity"
android:theme="#style/SplashTheme"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Remove setContentView() in SplashScreenActivity's onCreate() Method and use it as a java file which extends AppCompactActivity
The problem is because system process draws initial blank screen when launching the app, from documentation:
A common way to implement a themed launch screen is to use the windowDisablePreview theme attribute to turn off the initial blank
screen that the system process draws when launching the app. However,
this approach can result in a longer startup time than apps that don’t
suppress the preview window. Also, it forces the user to wait with no
feedback while the activity launches, making them wonder if the app is
functioning properly.
You can disable it with windowDisablePreview attribute, something like this:
<application
...
android:windowDisablePreview="true">
...
</application>
Splash Activity usually starts with the background.
Try this code, it might help you.
Code for your splash activity page.
public class SplashScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
Thread myThread = new Thread(){
#Override
public void run() {
try {
sleep(3000);
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}}
In layout file, you need to include just Image which you want to see on the splash page.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_splash_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/capture"
/>
</RelativeLayout>
And make sure you add the Splashscreen page before Main activity in Manifest.
<activity android:name=".SplashScreen">
Update
I changed the logic as pointed out by #dunnololz in his answer. But now Splash always appears when clicking on launcher icon even though application is running. I was hoping first time when app is not running splash would show otherwise login activity show but this is not happening.
Here is my code:
manifest splash launcher activity:
<activity
android:name=".activities.Splash"
android:label="#string/app_name"
android:launchMode="singleInstance"
android:noHistory="true"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Black.NoTitleBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Splash.Java:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
startService(new Intent(Splash.this, IMService.class));
setContentView(R.layout.activity_splash);
Thread background = new Thread() {
public void run() {
try {
// Thread will sleep for 3 seconds
sleep(3 * 1000);
Intent intent = new Intent(getBaseContext(), Login.class);
// intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
finish();
} catch (Exception e) {
}
}
};
// start thread
background.start();
}
I am using this gist to determine whether app is running or not. I found it via this tutorial. So that class uses Application.ActivityLifecycleCallbacks for API level 14+ which is what I need.
What I want to do is:
If application is running either in foreground or background, start login activity
If application is not running, start splash activity
Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent intent = new Intent(getBaseContext(), Splash.class);
if (Foreground.get(this).isForeground()
|| Foreground.get(this).isBackground()) {
intent = new Intent(EntryPoint.this, Login.class);
}
startActivity(intent);
finish();
}
And here is my application class:
public class MyApp extends Application {
#Override
public void onCreate() {
super.onCreate();
Foreground.init(this);
}
}
Of course I have added my app to manifest file as well:
<application
android:name=".MyApp"
The problem is that Splash activity is never started. It is the Login activity which is always started.
I just want to be able to do this:
If application is running either in foreground or background, start login activity
If application is not running, start splash activity
But not sure how to get this working, I might be missing something obvious here. Or even it might be that my requirement of either in foreground or background is wrong as I am new to Android.
Thanks for the help
Generally in Android it is not advised to determine behavior based on whether the app is currently being kept in memory or not. That is what the OS should be concerned about and not something you want to concern yourself about.
In the current version of Android (since I'm not sure if this behavior is true for older versions), when you tap an app to launch it, the OS checks to see if the app is already "open". If it is, it restores the state of the "open" app. If the app is not already "open", Android will launch the activity that is marked at the launcher. So the solution to this problem is simply to let Android handle it. Make a splash activity and mark it as the launcher in your AndroidManifest.xml. Then have the launcher open your login activity after some time.
Now, if the app is already open, Android simply will restore the last open activity. Otherwise it will show the launcher.
I have an app that has two activities.
The first one is presented with a single button that opens the second one.
Here is the Manifiest definition for the first one:
<activity
android:name="com.example.buttonexample.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Second activity:
<activity
android:name="com.example.buttonexample.MainActivity2"
android:label="#string/title_activity_main_activity2" android:theme="#android:style/Theme.Translucent">
</activity>
Here is how I launch the second activity (via OnClickListener for a button on the first activity):
public void startSecondActivityClick(View v) {
Intent startActivity2 = new Intent(this, MainActivity2.class);
startActivity(startActivity2);
}
This works fine, however when I background the app by hitting home and the foreground the app. I'm noticing that the first activity is continually creating/destroying itself. I verified this by putting some code in the onDestory method to increment a static int:
private static int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
count++;
}
protected void onDestroy() {
super.onDestroy();
Log.i("MainActivity", String.format("Destroyed, %d", count));
}
I've also noticed that removing the translucent theme seems to fix this. My question is is there a way to translucent or something similar but also have it not restart? Also, I'm curious why this happens at all. I'm testing this on 4.0.1 ICS on a galaxy SIII.
Ok after some digging I was able to figure out why this is happening. Someone had turned on one of the developer options, "do not keep activities.". After turning this off this stopped happening. I suspect this wouldn't happen in production too often as most people probably don't have that setting on. You can find this under settings -> "developer options" on most phones.
First of all let me tell you guys that I am completely newbie to Android app development.
I want to know how to launch home screen and then application home screen.
i.e whenever you click on some app, first it will show some logo screen and then automatically jump to home screen of the app.
I have my home screen of the app running, I just want to add some kind of logo screen when user click on the app.
I did little bit research on this and I found out
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
If I add this to logo activity then how to call app_home_screen activity automatically
Hope I am clear enough to explain the question.
You need to:
Add a new Activity SplashActivity which will be the start Activity of your app.
When your app starts, launch your SplashActivity and wait for some time.
When you finish waiting, start your MainActivity and finish the SplashActivity.
Here is a good example:
SplashScreenActivity:
public class SplashScreenActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout); //You need to define it in your layouts
final int welcomeScreenDisplay = 3000; //splash lasts for 3 sec. You can change it
/** create a thread to show splash up to splash time */
Thread welcomeThread = new Thread() {
int wait = 0;
#Override
public void run() {
try {
super.run();
while (wait < welcomeScreenDisplay) {
sleep(100);
wait += 100;
}
} catch (Exception e) {
System.out.println("EXc=" + e);
} finally {
startActivity(new Intent(SplashScreenActivity.this, MainScreenActivity.class));
finish();
}}
};
welcomeThread.start();
}
}
And don't forget to add the activities to your Manifest file:
<activity
android:name=".SplashScreenActivity"
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=".MainScreenActivity"
android:label="#string/app_name" >
</activity>
Although the other answers certainly work, here is an alternative to avoid having a separate activity just for your logo:
Place your ImageView as you would like (i.e. covering your full screen) before the other elements in your main activity layout (they will have an initial layout outside of the screen).
After calling setContentView in onCreate, run SystemClock.sleep(LOGO_TIME), where LOGO_TIME is an integer representing the number of milliseconds you want your logo to appear.
Finally, call setVisibility(View.GONE).
Your ImageView will be gone, and the other layout elements will fall into its place.
One solution you could attempt is to load the logo as your main activity, and have the application pause for a few seconds before calling the 2nd activity, your 'true' home page.
To start that 2nd activity you'll use 'Intent' such as
Intent myIntent = new Intent(myFirstActivity.this, realHomePage.class);
myFirstActivity.this.startActivity(myIntent);
Just make sure to extend the Activity class in your home page class
Have the main activity that gets called when your app icon is clicked on be an activity which has a view with only your logo on it. Something simple, just a RelativeLayout and an ImageView. After a time delay, have this activity start your actual main activity with an intent.