After calling the SplashScreenActivity the Application minimizes - java

I have the following Problem: I want to create a Splash Screen, that I can call during my Game Loop (it is a "Next-Level" splash screen).
When I call the Splash Screen, it works with animation and everything but the app minimizes itself after 5 seconds. This Splash Screen call happens after the main activity was already started.
My SplashActivity looks like this:
public class SplashActivity extends AppCompatActivity {
private ImageView logo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
logo = findViewById(R.id.logo);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
finish();
}
}, 5000);
Animation anim = AnimationUtils.loadAnimation(this, R.anim.viewanimation);
logo.startAnimation(anim);
}
}
And I want to call it in my GameLoop with the following call:
Intent i = new Intent(gamePanel.getContext(), SplashActivity.class);
gamePanel.getContext().startActivity(i);
The SplashScreen opens and the animation works, but then the MainActivity should be present again (not started new).

Try this one Splash.java
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Thread myThread=new Thread(){
#Override
public void run() {
try {
Animation anim = AnimationUtils.loadAnimation(this, R.anim.viewanimation);
logo.startAnimation(anim);
//sleep time after animation
sleep(3000);
//to end the splash screen and call next activity with Intent
finish();
Intent intent = new Intent(Splash.this,MainActivity.class);
startActivity(intent);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
myThread.start();
}
}
In your manifest file, Splash.java will be your first activity to launch,
<activity android:name=".Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
Hope it helps.

using finishActivity() instead of finish().
finish() Call this when your activity is done and should be closed. The ActivityResult is propagated back to whoever launched you via onActivityResult().
finishActivity(int requestCode) is used to finish another activity that you had previously started with startActivityForResult(Intent, int)

Related

Launcher Activity and Thread run() is called twice

My app is starting twice and the weird thing is, it depends on the LOAD_TIME which activity is called twice. With a long delay, like >5000ms, my Main Activity is called twice. As you can see in the snap shot, with 2000ms, my startup activity and my run() Thread is called twice.
It also only happens on my emulator, if I use my physical phone everything runs fine. I thought it might has something to do with the Consumer class, which requires a newer build version. But I think it requires 24 and I am using Nexus 5 with version 30 so I should be fine.
Ami doing something wrong with the lifecycle or threading?
public class StartUpActivity extends AppCompatActivity {
public final int LOAD_TIME = 2000;
public static int counter;
final Handler handler = new Handler(Looper.getMainLooper());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
counter++;
Log.d("debug counter startup: ", counter+"");
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_start_up);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titel_bar);
handler.postDelayed(new Runnable() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void run() {
Log.d("debug Handler: ", "run()");
PreLogin checkLoginStatus = new PreLogin();
checkLoginStatus.CheckIfStillLoggedIn((value) -> {
if(value==true){
Log.d("debug checkLogin", "checkIfStillLoggedIn=true");
Intent intent = new Intent(StartUpActivity.this, MainActivity.class);
intent.putExtra("caller", "StartUpActivity");
startActivity(intent);
finish();
}
else{
Log.d("debug checkLogin", "checkIfStillLoggedIn=false");
Intent intent = new Intent(StartUpActivity.this, ChooseLoginOrRegistrationActivity.class);
intent.putExtra("caller", "StartUpActivity");
startActivity(intent);
finish();
}
});
handler.removeCallbacks(null);
}
}, LOAD_TIME);
}
}
AndroidManifest:
<activity
android:name=".StartUpActivity"
android:launchMode="singleTask">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

Android studio back goes to splash screen

I have created this simple splash screen ;
public class Splashscreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splashscreen);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splashscreen.this, MainActivity.class);
startActivity(i);
finish(); } }, 1000);
}
}
I declared the splash screen to be the first screen that opens like this;
<activity android:name="com.androidhunger.opendagapp.Splashscreen"
android:noHistory="true"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"><intent-
filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter></activity>
After the splash screen my mainactivity opens a fragment with menu code inside of it, but I don't think the problem lays there. My problem is, when I open my app and the splash screen is done, and i've loaded my first screen, when I press back once, it takes me to a blank page. I can't recreate it here with easely, but I hope someone recognizes the issue.
you need to clear your activity stack before you start new activity
Intent i = new Intent(Splashscreen.this, MainActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(i);

onkeyDown / onBackPressed are not working with intent?

This is a simple operation and seems to be working until I've changed my minSdk = 8 to 9 , and targetSdk remains 21 in my manifest.
Problem is that i have an activity A , and i am going to activity B when a button is pressed in activity A . Now on activity B whenever someone press back button i want to clear the activity stack and transfer my activity B to activity C . but instead it finish activity B and go to Activity A , so far i have tried onKeyDown , onBackPressed , nothing seems to work , kindly help.
Activity A (in onClick method):
Intent in = new Intent(A.this,
B.class);
in.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP
| Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(in);
[UPDATED]Activity B :
#Override
public void onBackPressed() {
Intent in = new Intent(this, C.class);
Bundle bundle = new Bundle();
bundle.putSerializable("Scores", score);
in.putExtras(bundle);
startActivity(in);
}
Activity C:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.C);
ScoreSaver scores = (ScoreSaver) getIntent().getExtras().getSerializable("Scores");
}
Add android:noHistory="true" to both Activity A and B in your manifest file. This way these activities won't be in your back stack.
In Activity B you have to override the behavior of onBackPressed().
It has to look like this:
#Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
If you leave super.onBackPressed() in your method, the default behavior will happen, i.e. Activity B will be closed and you jump back to Activity A.
Issue was with Serializable object which was using storing context which will be null in next activity , which causes exception , that crashes my activity. so i removed it everything works fine. Thanks #Chris Fox
Unfortunately I do not have a Git Account, so I post the files' content here. (The app's theme inherits from Theme.AppCompat.Light.DarkActionBar)
Manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="fox.chris.activitytest">
<application android:allowBackup="true"
android:label="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:theme="#style/AppTheme">
<activity
android:name=".ActivityA"
android:noHistory="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".ActivityB"
android:noHistory="true">
</activity>
<activity android:name=".ActivityC">
</activity>
</application>
</manifest>
Activity A:
public class ActivityA extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(ActivityA.this, ActivityB.class);
startActivity(intent);
}
});
}
}
Activity B:
public class ActivityB extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
}
#Override
public void onBackPressed() {
Intent intent = new Intent(this, ActivityC.class);
startActivity(intent);
}
}
Activity C:
public class ActivityC extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_c);
}
}

Running background service through shortcut

So I want to launch a service from a shortcut. I know that this is not possible to do directly, so I've set up a activity with the sole purpose of starting the service.
The aim of my service is to send an intent to another app and then 5 seconds later send another so I've used a CountDownTimer to do this.
However, when I launch the Activity that starts the service from the shortcut (this is getting confusing) it launches the apps UI. I don't want this, as I want it to be a background service.
What am I doing wrong. I've only just got into development, so it could be something obvious, but I've been battling with this for a few days now.
For some reason when I run it from the service it just launches the app straight away...
When I run it straight from the invisible activity it runs properly for the 1st 5 seconds fine and then loads the app...
I can't figure out why it's loading the app at all.
I've included as much info as I can that would be relevant.
Any help is appreciated!
My service:
public class Pop1_5Service extends IntentService {
public Pop1_5Service() {
super("Pop1_5Service");
}
#Override
protected void onHandleIntent(Intent intent) {
// Normally we would do some work here, like download a file.
// For our sample, we just sleep for 5 seconds.
new CountDownTimer(5000, 2500) {
public void onTick(long millisUntilFinished) {
Intent i = new Intent(INTENT_ACTION);
Bundle b = new Bundle();
b.putInt(BUNDLE_VERSION_CODE, 1);
b.putString(BUNDLE_STRING_NAME, "POP1");
b.putString(BUNDLE_STRING_VALUE, "1");
i.putExtra(BUNDLE_NAME, b);
sendBroadcast(i); }
public void onFinish() {
Intent i = new Intent(INTENT_ACTION);
Bundle b = new Bundle();
b.putInt(BUNDLE_VERSION_CODE, 1);
b.putString(BUNDLE_STRING_NAME, "POP1");
b.putString(BUNDLE_STRING_VALUE, "1");
i.putExtra(BUNDLE_NAME, b);
sendBroadcast(i); }
}
}.start();
}
}
Activity that launches service:
public class Pop1_5Activity extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
Intent intent = new Intent(this, Pop1_5Service.class);
startService(intent);
finish();
}
}
Subsection of Manifest:
<activity
android:name=".Pop1_5Activity"
android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".Pop1_5Service" />
And the 'Create a Shortcut' Activity:
public class CreateShortcutActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent shortcutintent = new Intent(this, Pop1_5Activity.class);
ShortcutIconResource iconResource = Intent.ShortcutIconResource.fromContext(this, R.drawable.ic_launcher);
Intent intent = new Intent();
intent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutintent);
intent.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Pop1_5");
intent.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, iconResource);
setResult(RESULT_OK, intent);
finish();
}
}
From the look of things, it looks like CreateShortcutActivity does nothing.
Your LAUNCHER is Pop1_5Activity, so when the user presses the app icon, this Activity will run, and it launches the Service.
All the code you have showed us are "invisible", the two Activities finish() themselves, and the Service is a Service.
You might want to look at how your BroadcastReceiver handles your broadcast. For instance, does it create another Activity through PendingIntent? Is the Activity created invisible?
Maybe you should try creating a pending Service instead of pending Activity in the BroadcastReceiver.

Android Splash screen to activity

I am creating an android app and when I go to debug it on my samsung galaxy the Splash activity loads first,as it should, but after that the app crashes/stops right after doing the "Splash" activity. It doesn't go to the "MainActivity" activity after the thread sleeps for 5 seconds. Does anyone know what might be causing the problem? Plus after I tried debugging the app and loaded it onto my phone the app isn't even showing up. I am using Eclipse by the way. It shows the app in my application manager on my phone but it doesn't show the icon in my app screen.
Here is my Splash.java:
package com.example.mihirsandroidapp;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(5000);
} catch (InterruptedException e){
e.printStackTrace();
}finally{
Intent openMainActivity = new Intent("com.example.mihirandroidsapp.MAINACTIVITY");
startActivity(openMainActivity);
}
}
};
timer.start();
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
Here is my Manifest:
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/cartooncat"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".Splash"
android:label="#string/app_name" >
<intent-filter>
<action android:name="com.example.mihirsandroidapp.SPLASH" />
<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.example.mihirsandroidapp.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
And here is my main activity which should start after the splash screen:
package com.example.mihirsandroidapp;
import android.app.Activity;
import android.os.Bundle;
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 v) {
counter += 1;
display.setText("Total is " + counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter -= 1;
display.setText("Total is " + counter);
}
});
}
}
Oh.. Where do I start.. Let's go through all of the issues:
1) Fix your manifest. Definitely not the right way to declare your activities. Here is what it should look like:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/cartooncat"
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" >
</activity>
</application>
2) Now let's fix the way you start your activity:
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
3) Don't call finish() in onPause() - you break native activity lifecycle flow. Call finish() right after you start new activity:
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
4) Instead of creating separate thread, just a create a Handler and post Runnable there with 5 seconds delay:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//this will be called after 5 seconds delay
}
}, 5000);
Here is entire file put together:
public class Splash extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
Intent openMainActivity = new Intent(Splash.this, MainActivity.class);
startActivity(openMainActivity);
finish();
}
}, 5000);
}
If it doesn't help - we definitely need to look at logcat output...
A simple way to achieve this if one is ready to compromise on drawing performance is to define custom theme with splash image that one wants to use as window background and use this custom theme as application theme
styles.xml
<resources>
<style name="CustomTheme" parent="android:Theme">
<item name="android:windowBackground">#drawable/background_image</item>
<item name="android:windowNoTitle">true</item>
</style>
</resources>
AndroidManifest.xml
<application
android:debuggable="true"
android:icon="#drawable/icon"
android:theme="#style/CustomTheme"
android:label="#string/app_name">
...
</application>
This would use the #drawable/background_image as the window.background. As a result if the activities has transparent background then #drawable/background_image will be visible as activities background. One can avoid this by setting appropriate color or drawable in onCreate of every activity programatically as
public void onCreate(){
super.onCreate(savedInstanceState);
setContentView(R.layout.layoutResID);
activity.getWindow().setBackgroundDrawableResource(R.color.window_bg);
}
Check this for more information
All what you need for a splash screen
SplashActivity.java
public class SplashActivity extends AppCompatActivity {
private final int SPLASH_DISPLAY_DURATION = 1000;
#Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
new Handler().postDelayed(new Runnable(){
#Override
public void run() {
Intent mainIntent = new Intent(SplashActivity.this,MainActivity.class);
SplashActivity.this.startActivity(mainIntent);
SplashActivity.this.finish();
}
}, SPLASH_DISPLAY_DURATION);
}}
In drawables create this bg_splash.xml
<?xml version="1.0" encoding="utf-8"?><layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:drawable="#color/app_color"/>
<item>
<bitmap
android:gravity="center"
android:src="#drawable/ic_in_app_logo_big"/>
</item></layer-list>
In styles.xml create a custom theme
<style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
<item name="android:windowBackground">#drawable/bg_splash</item>
</style>
and finally in AndroidManifest.xml specify the theme to your activity
<activity
android:name=".activities.SplashActivity"
android:label="#string/app_name"
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>
Cheers.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Thread th = new Thread(new Runnable() { /*create a new thread */
#Override
public void run() { /*
* The purpose of this thread is to
* navigate from one class to another
* after some time
*/
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
/*
* We are creating this new thread because we don’t
* want our main thread to stop working for that
* time as our android stop working and some time
* application will crashes
*/
e.printStackTrace();
}
finally {
Intent i = new Intent(MainActivity.this,
Splash_Class.class);
startActivity(i);
finish();
}
}
});
th.start(); // start the thread
}
http://www.codehubb.com/android_splash_screen
I have added splash screen by using following code:
public class SplashActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash_layout);
initConstatnts();// method for intilizing any constants
new Thread(new Runnable() {
#Override
public void run() {
if (!isFinishing()) // checking activity is finishing or not
{
try {
Thread.sleep(3000);//delay
Intent i = new Intent(getBaseContext(),
HomeActivity.class);
startActivity(i);
finish();
}
catch (InterruptedException e)
{
e.printStackTrace();
}
}
}
}).start();
}
private void initConstatnts() {
}
}
Flow the below steps to Create your own splash screen
Create Activity1 and XML file
Design the XML file and put the welcome message
Crete Activity2 and XML file.
Start the Activity1
Start a Thread in Activity1 and sleep for 5 seconds
Start Activity2 from the Thread
There is nothing called readymade splash screen in Android . we can achieve that using above steps.
In a single line, start Activity1 wait for 5 sec then start Activity2.
So user will feel that first screen is splash screen.
You can download the complete code from below link
http://javaant.com/splash-screen-android/#.VwzHz5N96Hs
i have faced the same problem.... i think you code is perfect for the app
u just try with this in the Intent Creation in your splash activity class
Intent openMainActivity = new Intent("android.intent.action.MAIN");//MAIN is the that u want to start
//next after the current Activity

Categories

Resources