The splash activity shows just blank screen. Code is below :
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
public class SplashScreen extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash_screen);
try {
Log.v(" gonna AsyncTask"," execute AsyncTask");
new AsyncTask<String, String, String>() {
#Override
protected void onPreExecute() {
Log.v(" PreExecute AsyncTask"," execute AsyncTask");
}
#Override
protected String doInBackground(String... arg0) {
Log.v(" in doInBackground"," execute AsyncTask");
try {
Log.v(" sleep doInBackground"," execute AsyncTask");
Thread.sleep(SPLASH_TIME_OUT);
} catch (Exception e) {
Log.v(" Exception "," execute AsyncTask");
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String unused) {
Log.v(" onPostExecute "," execute AsyncTask");
Intent i = new Intent(SplashScreen.this,
MainActivity.class);
Log.v(" starting Activity "," execute AsyncTask");
startActivity(i);
}
}.execute();
} catch (Exception e) {
e.printStackTrace();
}
}// end of onCreate
}// end of class SplashScreen
The XML layout file has :
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.istiaqueahmed.archeology.SplashScreen">
<ImageView
android:id="#+id/splash_img"
android:layout_width="170dp"
android:layout_height="112dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="130dp"
android:src="#drawable/splash"
/>
<TextView
android:id="#+id/developed_by"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="#string/developed_by"
android:layout_below="#id/splash_img"
android:textStyle="bold"
android:textSize="13sp"
android:layout_alignParentBottom="true"
android:layout_marginBottom="30dp"
/>
</RelativeLayout>
Android manifest file has -
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
>
<activity
android:name=".SplashScreen"
android:noHistory="true"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
........
</application>
And the style.xml has -
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Perhaps the activity needs some time for setContentViewto execute and meanwhile the app goes to the MainActivity.
How to prevent the app from showing the blank screen and instead show the splash screen for the intended time period?
Your App is freeze because you are sleeping main Thread.
Thread.sleep(SPLASH_TIME_OUT); // This will sleep your main thread.
Instead of AsyncTask use Handler with delay.
Use CountDownTimer instead of AsyncTask.
private static long SPLASH_TIME_OUT = 3000;
new CountDownTimer(SPLASH_TIME_OUT, 1000) {
public void onTick(long millisUntilFinished)
{
}
public void onFinish()
{
Intent i = new Intent(SplashScreen.this,
MainActivity.class);
startActivity(i);
}
}.start();
There is no need to used AsyncTask here.
Please try this code which helps you.
public class WelcomeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_welcome);
startHandler();
}
private void startHandler() {
Runnable runnable = new Runnable() {
#Override
public void run() {
Intent i = new Intent(WelcomeActivity.this, MainActivity.class);
startActivity(i);
finish();
}
};
Handler handler = new Handler();
handler.postDelayed(runnable, 3000);
}
}
Related
Who can explain the following behavior?
First activity:
public class MainActivity extends AppCompatActivity {
public void onClick(View view) {
startActivity(new Intent(this, SecondActivity.class));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onStart() {
super.onStart();
Log.d(">>><<<", "onStart: " + getClass().getSimpleName());
}
#Override
protected void onStop() {
super.onStop();
Log.d(">>><<<", "onStop: " + getClass().getSimpleName());
}
#Override
protected void onPause() {
super.onPause();
Log.d(">>><<<", "onPause: " + getClass().getSimpleName());
}
#Override
protected void onResume() {
super.onResume();
Log.d(">>><<<", "onResume: " + getClass().getSimpleName());
}
}
Second activity:
public class SecondActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
#Override
protected void onStart() {
super.onStart();
Log.d(">>><<<", "onStart: " + getClass().getSimpleName());
}
#Override
protected void onStop() {
super.onStop();
Log.d(">>><<<", "onStop: " + getClass().getSimpleName());
}
#Override
protected void onPause() {
super.onPause();
Log.d(">>><<<", "onPause: " + getClass().getSimpleName());
}
#Override
protected void onResume() {
super.onResume();
Log.d(">>><<<", "onResume: " + getClass().getSimpleName());
}
}
Layout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:onClick="onClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click here!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SecondActivity"
android:theme="#style/Transparent"/>
</application>
</manifest>
Styles:
<style name="Transparent" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
</style>
Steps to reproduce:
Turn on "Don't keep activities" option in "Developer options" for simple reproducing.
Launch the app and press the button to call the second activity.
Press Home button.
Go back to the app.
Expected logs after step #4:
onStart: MainActivity
onStart: SecondActivity
onResume: SecondActivity
Actual logs after step #4:
onStart: MainActivity
onResume: MainActivity
onStart: SecondActivity
onResume: SecondActivity
Why MainActivity resumes? Why doesn't pause after resume before start SecondActivity?
I added splash screen to my application and my code looks as follow:
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
private Handler handler = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
}
My Manifest :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.vdovin.currencyratesapp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:name=".application.CurrencyApp"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".screens.splash.SplashActivity"
android:theme="#style/SplashTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".screens.main.CurrencyExchangeActivity">
</activity>
</application>
</manifest>
So I faced with next problem:
If I hide my app with home button when loading splash screen, then when I open app again, splash screen activity not call CurrencyExchangeActivity. I understand that it appears because method onCreate() invoked only once, but I can't put it in onResume() because when I open my app again it shows me the splash screen again. But I want to show CurrencyActivity, like google's apps(maps, sheets etc...)
You cannot do that simply because you're setting the splash screen image as background of theme of your Activity. That's window level background which would show up regardless of your navigation or anything.
If you really want to show splash screen only once in app lifetime then you'll have to do it in not the right way and have that image as part of your layout and inflate that layout using setContentView. Now when you come again in the app, you will call your CurrencyExchangeActivity even before setContentView of splash activity and which will just show a black window background and directly show up CurrencyExchangeActivity.
Let me know if this makes sense or not. I can elaborate more if needed
Please try this way. I haven't tested your code. My sway of using the splash will be like below:
public class SplashActivity extends Activity {
private static final String TAG = SplashActivity.class.getSimpleName();
Handler handler = new Handler() {
#Override
public void handleMessage(Message msg) {
loadNext();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Thread() {
#Override
public void run() {
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
handler.sendEmptyMessage(1);
}
}.start();
}
protected void loadNext() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
}
you have forgot to add
setContentView(R.layout.splash);
add this it will work
Maybe you could try the following:
#Override
protected void onResume() {
super.onResume();
Intent intent = new Intent(this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
This will skip the delay when reopening the app after pressing the home button.
I usually use a postDelayed Runnable.
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
private Handler handler = new Handler();
private Runnable runnable = new Runnable() {
#Override
public void run() {
Intent intent = new Intent(this, NextActivity.class);
startActivity(intent);
finish();
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
protected void onResume() {
super.onResume();
// 5 sec
handler.postDelayed(runnable, 5000);
}
#Override
protected void onPause() {
super.onPause();
// 5 sec
handler.removeCallbacks(runnable);
}
}
I would like to suggest to keep the splash screen showing mechanism in the CurrencyActivity in your case. Here's the pseudo code.
I've added proper comments in the code. Please check.
public class CurrencyActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000; //for testing i use 5 seconds
private Handler handler = null;
private boolean showSplash = true; // True by default
private ImageView splashImage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency_activity);
// Get a image overlaying the other views in your currency layout
splashImage = (ImageView) findViewById(R.id.splash);
if(showSplash) showSplashScreen();
}
#Override
protected void onStop() {
super.onStop();
handler.removeCallbacksAndMessages(null);
}
#Override
protected void onPause() {
super.onPause();
showSplash = false; // Set the variable to false when you take this in background
}
#Override
protected void onResume() {
super.onResume();
// Check if the variable is false and then set the visibility to GONE
if(!showSplash) splashImage.setVisibility(View.GONE);
}
private void showSplashScreen() {
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
splashImage.setVisibility(View.GONE);
showSplash = false;
}
}, DELAY_MILLIS);
}
}
Try this out--
SplashScreenActivity-
public class SplashActivity extends AppCompatActivity {
public static final int DELAY_MILLIS = 2000;//for testing i use 5 seconds
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, CurrencyExchangeActivity.class);
startActivity(intent);
finish();
}
}, DELAY_MILLIS);
}
}
For manifest--
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".CurrencyExchangeActivity"/>
</application>
i tested this code for if you had the problem that when the home screen button is pressed the splash screen is displayed again then this would work....
I have a launcher activity followed by loginActivity. But everytime I start my APP my loginActivity is fired up first rather than my launcherActivity.
The xml for manifest.xml is like
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kryptapps.konel.dchat2" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/CustomTheme" >
<activity
android:name=".LauncherActivity"
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=".LoginActivity"
android:label="#string/title_activity_login" >
</activity>
<activity
android:name=".GroupListActivity"
android:label="#string/title_activity_group_list" >
</activity>
</application>
</manifest>
java file is like :
public class LauncherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
loadPersonalDatas();
Thread time = new Thread() {
public void run() {
try {
sleep(20000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
time.start();
}
void loadPersonalDatas(){
new Thread() {
public void run() {
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
the login activity is like usual log in page.
Actually LauncherActivity is getting fired, but just for 500 milliseconds.
Whats happening is the wait for 20000 milliseconds and the execution of loadPersonalDatas() is carried out simultaneously.
But after 500 milliseconds, the activity changes and sleep(20000) is interrupted.
For the LoginActivity to launch after 20000 milliseconds, you should start the LoginActivity after the sleep(20000) in the run() method.
public class LauncherActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_launcher);
Thread time = new Thread() {
public void run() {
try {
sleep(20000);
loadPersonalDatas(); //start activity here
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
time.start();
}
void loadPersonalDatas(){
new Thread() {
public void run() {
try {
sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}.start();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
Now, the LoginActivity will start after 20500 milliseconds.
I have been trying to add splash screen like this one in my android app but the app keeps crashing whenever i try to run it. here are the codes
activity_splash.xml
<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=".Splash"
<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:id="#+id/imageView"
android:background="#drawable/cricket"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
**AndroidManifest.xml**
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat" >
<activity
android:name=".Splash"
android:label="Splash"
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>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
</application>
Here is my java code for splash activity
Splash.java
public class Splash extends AppCompatActivity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler() {
public void postDelayed(Runnable runnable, int splashTimeOut) {
}
#Override
public void close() {
}
#Override
public void flush() {
}
#Override
public void publish(LogRecord record) {
}
}.postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
update your handler
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent i = new Intent(Splash.this, MainActivity.class);
startActivity(i);
finish();
}, SPLASH_DISPLAY_LENGTH);
}
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
public class SplashScreen extends Activity {
// Splash screen timer
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed(new Runnable() {
/*
* Showing splash screen with a timer. This will be useful when you
* want to show case your app logo / company
*/
#Override
public void run() {
// This method will be executed once the timer is over
// Start your app main activity
Intent i = new Intent(SplashScreen.this, MainActivity.class);
startActivity(i);
// close this activity
finish();
}
}, SPLASH_TIME_OUT);
}
}
I am having an issue where the application does not load after including the Splash screen to the application. This is the code for the SplashScreen.java & the Manifest file. I am not sure what I am missing. I have gone over this several times and am unable to spot the error. I have gone thro' similar posts but could find the answer. Please help
public class SplashScreen extends Activity {
private long ms=0;
private long splashTime=2000;
private boolean splashActive = true;
private boolean paused=false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread mythread = new Thread() {
public void run() {
try {
while (splashActive && ms < splashTime) {
if(!paused)
ms=ms+100;
sleep(100);
}
} catch(Exception e) {}
finally {
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}
};
mythread.start();
}
The Manifest file is as follows
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.tvganesh.totalcontrol.SplashScreen"
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="com.tvganesh.totalcontrol.TotalControl"
android:label="#string/app_name" >
</activity>
</application>
After the Splash Screen which it display it is supposed to switch to TotalControl.class. But the I get the message "The Application stopped unexpectedly"
Can you let me know what I am missing?
Don't use Thread.sleep - you have no guarantee that it will start up again exactly after 100ms. Use a Handler instead:
getWindow().getDecorView().getHandler().postDelayed(new Runnable()
{
#Override
public void run()
{
Intent intent = new Intent(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
}, splashTime);
I think this will work for you
Don't use threads to show the splash.
public class SplashScreen extends Activity {
protected int _splashTime = 5000;
private Thread splashTread;
MyCount counter = new MyCount(4000, 4000);
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
counter.start();
}
public class MyCount extends CountDownTimer
{
public MyCount(long csecond, long countDownInterval)
{
super(csecond, countDownInterval);
}
#Override
public void onFinish() {
finish();
Intent intent = new Intent();
intent.setClass(SplashScreen.this, TotalControl.class);
startActivity(intent);
}
#Override
public void onTick(long arg0) {
}
}
}
I have solved the problem. This appears to be a known bug in AndEngine and the fix is add the following line to the Manifest file
android:configChanges="orientation|screenSize"
Thanks for all your help.