I am wondering why my application ignores my SplashScreen.java activity when resuming the aplication. If I close it with the "Back" button, the splash screen comes up on start, but if I exit with the home button the SplashScreen activity is not being called...:(
I even added the onResume event, but the splash screen still wont come up when resuming my app.
Thanks!!
SplashScreen.java
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
spashStart();
}
protected void onResume(){
super.onResume();
spashStart();
}
private void spashStart() {
Thread splashTimer = new Thread() {
public void run(){
try{
sleep(5000);
Intent mainActivity = new Intent("com.exploreca.tourfinder.MainActivity");
startActivity(mainActivity);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
finish();
}
}
};
splashTimer.start();
}
}
Maifest:
...
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/scena_logo"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.exploreca.tourfinder.Splash"
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=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.exploreca.tourfinder.MainActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".SettingsActivity"
android:parentActivityName="com.exploreca.tourfinder.MainActivity">
</activity>
<activity
android:name=".TourDetailActivity"
android:parentActivityName="com.exploreca.tourfinder.MainActivity">
</activity>
<activity
android:name=".NotificationDetails"
android:label="#string/title_activity_notifDetails_title"
android:parentActivityName="com.exploreca.tourfinder.MainActivity">
</activity>
<activity
android:name=".SavedEvents"
android:label="#string/title_activity_SavedEvents"
android:parentActivityName="com.exploreca.tourfinder.MainActivity">
</activity>
<activity
android:name=".FollowList"
android:label="#string/title_activity_Urmarite"
android:parentActivityName="com.exploreca.tourfinder.MainActivity">
</activity>
...
Try this..
change this..
Intent mainActivity = new Intent("com.exploreca.tourfinder.MainActivity");
startActivity(mainActivity);
to
Intent mainActivity = new Intent(Splash.this,MainActivity.class);
startActivity(mainActivity);
public class SplashActivity extends AppCompatActivity {
Handler handler;
private final int SPLASH_DISPLAY_LENGTH = 2000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SplashStart();
}
private void SplashStart() {
handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
#Override
protected void onResume() {
super.onResume();
}
}
/* just add on resume method inside do not start your splace method in `onResume` method...*/
when you click back button actually the activity is getting finished and when you comes back the splash screen shows. That because once more the appliaction is starting.
When you click home button the activity is not finishing but it goes to background and when you open the app oncemoe then the same activity is bought to front. so the splash screen is not shown.
its how when it works through the lifecycle of the application.
Try to call finish(); method in th onPause of the activity. So it will be finished all the time. Or try adding noHistory to the activity in the manifest. Hope this will help you.
Related
It's my project situation now.
MainActivity is LAUNCHER
SplashActivity called MainActivity onCreate()
When I think about it, it looks like there is no problem.
but after app starting,
The MainActivity screen is briefly visible before SplashActivity call.
Surprisingly, I did not see it on other devices, only galaxy s8.
Of course, I know it is not a general structure. But I can not understand it because I have been working normally.
white color is cold start style and splashActivity.
red color is mainActivity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// some getIntent code
startActivityForResult(new Intent(this, SplashActivity.class), RESULTCODE_);
setInitLayout();
}
manifest
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
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=".SplashActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
minSdkVersion 21
targetSdkVersion 28
If you use style with android:windowBackground for splash activity, don't call setContentView(). Thats all!
It is not a good idea to use a splash screen that way . This should be strictly avoided.
With this approach you may also lead the problem of blank white page appears during splash launching and this what exactly happened to you !
i advice you to read this article and try to make your splash screen in the right way to avoid such behavior !
You have placed MainActivity class to be the launcher activity in your manifest. I am assuming that your splashscreen activity is called SplashActivity. If you want it to be shown before as the SplashScreen and then MainActivity to come later on, change your manifest code to be:
<activity android:name=".SplashActivity"
android:screenOrientation="portrait"
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=".MainActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
<activity android:name=".IntroActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
android:screenOrientation="portrait"
android:theme="#style/SplashTheme" >
</activity>
Then create your SplashActivity as suggested by Ismail in the link that he has provided
simply add handler for limited seconds of time and finish current activity
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//actvity transaction
}
},3000);
Use this thread code for splash activity it will better work -
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread thread = new Thread() {
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainAct);
finish();
}
}
};
thread.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
and your manifest class will be look like this :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.example.diskapp">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/logo"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="AllowBackup,GoogleAppIndexingWarning">
<activity android:name=".Activities.SplashActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Activities.MainActivity" />
</application>
</manifest>
Use this thread code for splash activity. It will better work :
public class SplashActivity extends AppCompatActivity {
private static int SPLASH_TIME_OUT = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread thread = new Thread() {
public void run(){
try{
sleep(3000);
}catch (InterruptedException e){
e.printStackTrace();
}finally {
Intent mainAct=new Intent(SplashActivity.this, MainActivity.class);
startActivity(mainAct);
finish();
}
}
};
thread.start();
}
#Override
protected void onDestroy() {
super.onDestroy();
}
}
I was unable to find out how to show splash screen when starting app from shortcut. Shortcut is a direct path to open certain activity, so splash screen is bypassed. any idea how to include splash screen at every app start combinations ?
My splash implementation:
public class SplashScreen extends AppCompatActivity {
Context context = this;
/** Duration of wait **/
private final int SPLASH_DISPLAY_LENGTH = 2000;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
new Handler().postDelayed( new Runnable(){
#Override
public void run() {
/* Create an Intent that will start the Menu-Activity. */
Intent goToMainActivity = new Intent(SplashScreen.this, MainActivity.class);
goToMainActivity.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(goToMainActivity);
finish();
}
}, SPLASH_DISPLAY_LENGTH);
}
}
Manifest:
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:noHistory="true"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data android:name="android.app.shortcuts" android:resource="#xml/shortcut" />
</activity>
<activity
android:name=".MainActivity"
android:noHistory="true">
</activity>
Shortcut:
<shortcut
android:shortcutId="shortCutEnvironment"
android:enabled="true"
android:icon="#drawable/ic_action_temperature"
android:shortcutShortLabel="#string/shortcut_environment"
android:shortcutLongLabel="#string/shortcut_environment">
<intent
android:action="android.intent.action.VIEW"
android:targetPackage="com.app.myapp"
android:targetClass="com.app.myapp.EnvironmnetScreen"
/>
<category android:name="android.shortcut.conversation"/>
</shortcut>
I was having a play with Android Studio to create a birthday card and have come to a slight problem with the audio file playing before MainActivity is even on screen. The audio file plays while the splash screen is still on the screen.
I have two Java files:
MainActivity.class
package com.example.android.happybirthday;
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
MediaPlayer mySoundfile;
#Override
protected void onPause() {
super.onPause();
mySoundfile.release();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mySoundfile = MediaPlayer.create(this, R.raw.music);
mySoundfile.setLooping(true);
mySoundfile.setVolume(100, 100);
mySoundfile.seekTo(0);
mySoundfile.start();
}
}
and Splash.class
package com.example.android.happybirthday;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_screen);
Thread mythread = new Thread() {
#Override
public void run() {
try {
sleep(3000); // 3 second delay for cold start
Intent startMainApp = new Intent(getApplicationContext(), MainActivity.class); // initiate MainActivity
startActivity(startMainApp); // open MainActivity
finish(); // close this activity
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
mythread.start();
}
}
This is the AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.happybirthday">
<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=".Splash"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".MainActivity"
android:screenOrientation="portrait"
android:configChanges="keyboardHidden"
android:label="#string/app_name"
android:theme="#style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Could you kindly give some support?
Thanks :)
Thanks #Sergey and #You Kim, I managed to get it done via adding a delay in onStart within the MainActivity.class
you need to run the media player when the activity is started, not on the create.
try running media player on onStart() or onResume().
Called when activity resume is complete
protected void onPostResume()
I want to show my splash.xml for 6 seconds and then activity_main.xml but it is not showing the activity_main.xml...
my splash.java code:
package com.example.ne;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splash extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run(){
try{
sleep(6000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent open =new Intent("com.example.ne.MAINACTIVITY");
startActivity(open);
}
}
};
timer.start();
}
}
and mainactivity.java code:
package com.example.ne;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add,sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add=(Button) findViewById(R.id.bAdd);
sub=(Button)findViewById(R.id.bSub);
display=(TextView)findViewById(R.id.tvdisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
counter++;
// TODO Auto-generated method stub
display.setText("Your total is" + counter);
}
});sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
counter--;
display.setText("Your total is" + counter);
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and manifest.xml code:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ne"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.ne.splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity" android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.ne.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Not that you can't do it your way, but managing threads is trickier business than it may seem. That is why Android incorporated a number of helpers for it. In your case, you could use a Handler to post a delayed runnable that will act as your redirect.
new Handler().postDelayed(new Runnable(){
#Override
public void run(){
Intent open = new Intent(splash.this, MainActivity.class);
startActivity(open);
splash.this.finish();
}
}, 6000);
This will keep you from having to manage any threads and will close the splash activity after the intent is passed.
Second, if you will notice, the Intent instantiation, new Intent(splash.this, MainActivity.class), this is the way you should create Activity intents. This will provide context for the explicitly called intent.
Third, the class splash should be Splash, per Java naming conventions. Camel case is appropriate, but classes have an uppercase first letter, while methods and variables have a lowercase first letter.
And last, your manifest should look like this
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ne"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".splash"
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=".MainActivity"
android:label="#string/app_name" />
</application>
</manifest>
EDIT
As recommended by #JHH it is also possible to stop the splash page from redirecting on things like back pressed or the home screen. What you can do for that is:
First, make the class variables mHandler and mRunnable and when you instantiate your objects, let the variables reference them.
private Handler mHandler;
private Runnable mRunnable;
#Override
public void onCreate(Bundle savedInstanceState){
...
mHandler = new Handler();
mRunnable = new Runnable(){...};
...
}
Next, let the handler post the runnable message with a delay in onResume. This will allow us to create the post message every time the splash activity comes to the front (remember that after the redirect the splash screen will finish so this won't be an issue).
#Override
public void onResume(){
super.onResume();
postDelayed(mRunnable, 6000);
}
Lastly, in onPause we can then stop the delayed post like so. This will keep the handler from triggering the intent which will make it so the redirect doesn't happen if the splash screen is in the background. In other words, if you hit the splash screen then hit the back/home button the runnable won't continue and open the activity anyway.
#Override
public void onPause(){
mHandler.removeCallbacks(mRunnable);
super.onPause();
}
Try instead
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
Make following changes in your code
Package com.example.ne;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class splash extends Activity{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread()
{
public void run(){
try{
sleep(6000);
} catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent open =new Intent(splash.this,MainActivity.class);
startActivity(open);
}
}
};
timer.start();
}
}
is it possible to get a login activity in between splash and main activity in android studio.`it should be after splash activity and before main.how to get it.
<?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=".Splash"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Register"
android:label="#string/title_activity_register"
android:theme="#style/AppTheme.NoActionBar"/>
<activity android:name=".MainActivity"
android:theme="#style/AppTheme.NoActionBar"/>
</application>
You may change your manifest like this
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:logo="#drawable/icon"
>
<activity
android:name=".SplashActivity"
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=".LoginPage"></activity>
<activity android:name=".MainPage"></activity>
</application>
and make sure your intents are appropriare in order
When you call splash activity, inside splash activity just call loginactivity and if you are on login activity, on press of the button, call mainactivity. That's it.
public class SplashActivity extends Activity {
private final int STR_SPLASH_TIME = 3000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
startSplashTimer();
}
private void startSplashTimer() {
try {
Timer timer = new Timer();
timer.schedule(new TimerTask() {
#Override
public void run() {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
finish();
}
}, STR_SPLASH_TIME);
} catch (Exception e) {
e.printStackTrace();
}
}
From SplashActivity, it will be going to the LoginActivity, when this happened after that onclick of the button call MainActivity.
So your flow should be like this,
SplashActivity --> LoginActivity --> MainActivity
I've answered a similar question two or three weeks ago. Find the answer here.
However, in my opinion, creating an activity just for showing a splash is not so good. Essentially, your app should have as few activities as it can. Activities are, with no doubts, the main and heaviest component for an app. So, for just showing a splash for you may want to show that splash in your LoginActivity until further resources are loaded. Once all are loaded, you can hide that splash and show log-in form.
Do something like this:
SplashActivity.java
public class SplashActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_activity);
...
if(!alreadyLoggedIn()) {
Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
startActivity(intent);
} else {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
startActivity(intent);
}
}
}
LoginActivity.java
public class LoginActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.chat_activity);
if(loginSuccessful()) {
Intent intent = new Intent(LoginActivity.this, MainActivity.class);
startActivity(intent);
} else {
// show ERROR
}
}
}