Show Splash Screen on every launch of android application - android

I have splash screen in an application. When user is at home screen and presses the back button; an alert dialog with "Do you want to exit?" is appeared and if users slects OK then moveTaskToBack(true); is called: and the app will exit. But on immediate launch of the application the splash screen is not shown. How to show splash screen on every launch of the application?

A few pointers on how to make a splash screen.
Say we have SplashActivity and HomeActivity.
SplashActivity should be your launcher activity (in the manifest).
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
When you start HomeActivity you should call finish() on SplashActivity so that you wouldn't return to it upon clicking the back button.
In HomeActivity when you intercept the back button to show the dialog, don't call moveTaskToBack(true); on OK because this only moves your application to the background and doesn't terminate it.
You should call finish() instead. And since you already called finish on SplashActivity, the only activity left is HomeActivity, so your application will close.

Ideally You should not do that .You should respect user expectations for behavior of apps.When he clicks on your app it should start from where he left . If the app is killed due to memory constraints then it will start a fresh .
Still if you want to do it : Source : Close application and launch home screen on Android
Intent intent = new Intent(this, FinActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
Inside FinActivity :
call finish() in onCreate.

I think you SplashScreen is only shown when the App is starting.
So if the Acitivites aren't finished but just paused the SplashScreen will not be shown because the App wasn't really closed.
You can either finish the application when back button is pressed (Code: this.finish()) or check on every Activity's onResume()-Method if the App was in Background and show the SplashScreen if true.

I feel the only reason splash screens are ever necessary are for the launch of an application and if you want to load some information through a thread while doing so, but that is just my opinion.
Anyways, you should be launching your splash screen as the first activity in your manifest, then after you would launch an intent that will start your next activity?(The actual main). There is a great tutorial I used a couple of months ago when I was first dealing with these, It goes through it with you STEP by STEP.
I hope this helps :)
Tutorial I
Tutorial II

Related

Android skipping the splash screen when re-opening the app after having pressed back

So my scenario is as such.
Let's say there is a MainActivity, which only job is to start, call installSplashScreen().setKeepOnScreenCondition { true } to show the Splash screen using the new backward compatible APIs, and then after checking some state it does startActivity(SomeActivity); finish()
Now we're on the SomeActivity and if we press the home button, the app is gone on the background. Then if we click on the launched icon, the SomeActivity is launched correctly, and the MainActivity's onCreate is never called, therefore the splash screen does not show again, and the SomeActivity shows instantly.
But if instead of pressing the home button, we press the back button, and the app is backgrounded that way, then when we click on the launcher icon, the MainActivity's oncreate is called again, and the splash screen icon flashes for a tiny fraction too making it look jarring.
My question is, does this sound like it's some wrong configuration on my part, or am I stuck with this behavior as long as I am not on a single activity architecture?
You are confused. Pressing the BACK button does not "send the app to the background". The default behaviour of the BACK button (assuming that you don't override this and provide your own behaviour) is to finish the current Activity. Normally this will take the user to the previous Activity in the current task. In your case, there is no other Activity in the task stack, so the current task is empty. This may appear to the user as "the app is sent to the background", but in reality there is nothing in the background. Your app's task is empty and so it is gone.
When the user then taps the app icon on the HOME screen or taps on the app's task in the list of recent tasks, there is no existing task to bring to the foreground, so the app is started again by creating a new task and launching the root Activity (in your case MainActivity) into the newly created task.
If you want the BACK button to just put your app into the background (instead of calling finish() on SomeActivity, which is the default behaviour) then just override onBackPressed() in SomeActivity and do this instead:
moveTaskToBack(true);
It seems like there is no solution to what I am facing as long as the Activity I want to resume to is not the Launcher activity.
From the the docs:
"Root launcher activities are activities that declare an Intent filter with both ACTION_MAIN and CATEGORY_LAUNCHER. These activities are unique because they act as entry points into your app from the app launcher and are used to start a task.
System behavior on Android 12 and higher
The system moves the activity and its task to the background instead of finishing the activity. This behavior matches the default system behavior when navigating out of an app using the Home button or gesture."
Reading the docs about the new back behavior on Android 12 and onwards tells us that pressing back when you got nothing else on the stack will act as if you pressed the home button.
There's a big exception here, and that is that when you re-open the application, if the one you just popped was not the launcher activity, it will then still need to launch that launcher activity and can't resume from where you left off in a hot state, exactly the reason why I am seeing the splash screen again.
So I think my best bet is to either ignore this for now, or fix my app to be a single-activity app, or at least keep the launcher activity be the top-level one that you exit the app from by pressing back
To indicate a couple of examples, if one wants to experience what I mean, the reproduction steps are to:
Open the app
Press the back button which will send you out of the app to the home screen
Click on the app icon again
As of today, apps like Google Photos, and Google Podcasts don't show the splash again. In contrast, apps like Google Maps, Twitter, Spotify do show the splash again for a brief second.
to call the launcher activity every time you have to clear the stack that means you have to use a flag in your manifest to tell your app not to keep activity in background try android:launchMode="singleTask" inside your activity tag in manifest the activity that you want to be killed everytime you go to background, and as far as how much time splash should be showing you can use timer for that after the timer is finished then your someActivity will be called.

How can I make an activity on be openable when launched?

I have an app where my LAUNCHER activity, named dispatch activity, which sees whether the user has logged in before or not(in onCreate()), and then launches the correct activity. And when the correct activity is launched, I can tap the back button on my device and it goes back to the dispatch activity, and it stays there until the user refreshes the app. I only want the activity open-able by LAUNCHER. So if the user clicks the back button on the device, it stays there, like the dispatch activity doesn't exist. (By the back button I mean by that icon next to the home button on the tablet)
Hope you can help
Have you tried this : when you start the second activity, at the same time you exit (run finish()) on your dispatch activity?
EDIT: like this: (in your Dispatch Activity)
//quit your Dispatch Activity
finish();
//start new activity
startActivity(new Intent(this, YourNewActivity.class));
I'd recommend to play with intent flags. In this particular case, have a look at
FLAG_ACTIVITY_NO_HISTORY.
Set it for your dispatch activity right in the manifest and it won't be kept in the history stack.
Add LAUNCHER activity as a FLAG_ACTIVITY_NO_HISTORY.
Just define in your manifest file:
<activity android:name="LAUNCHER ACTIVITY"
android:noHistory="true"
...
></activity>
Now validate your user login stuff and just start your next activity. Rest of the stuff will be taken care by Android.

Application needs to restart every time it's entered

I have an application with a splash screen. It shows the first time I run but then it never shows again. Is there anyway of making my application reset every time I start it?
Your problem is that your activity will not been destroyed, when it goes into the background. Instead your app is just continues at this place where you left it. Just imagine that you switch between two activities you properly don't want to show in those cases the splash screen will been shown again. By default it would just continue your application until it is terminated in the background e.g. when the memory gets low.
However if you really want that behavior (which I would not suggest) then call in onPause()the method finish(). This will terminate your current instance, when your activity get into the background.
If you just want to show the splash screen on the app launcher icon click you should use the android:launchMode="singleInstance" option in the manifest, what also Lal pointed out in his answer. To detect that your application got started with the launcher icon you can check the intent.
On the first start just onCreate() will been called, on the second start (via the launcher) onNewIntent(Intent intent) will been called there you can check with Intent.CATEGORY_LAUNCHER.equal(intent.getAction()) if your app was opened with the launcher or not.
Make the splashscreen Activity the first activity of your application as below
In your AndroidManifest.xml add this to the SplashScreen Activity
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
UPDATE
Add android:launchMode="singleInstance" in your AndroidManifest.xml file inside those activities that you want to open only once.

How to start android activity without showing main activity in the back

I'm developing an application containing 2 activities:
Main activity launched where user starts the app.
Second activity is launched where user clicks on a button widget
on the home screen.
I created my widget and the view that will be displayed when the user clicks on the widget button.
My problem: The second activity UI is displayed above the main activity, the main activity is visible because the second activity layout is transparent, because I want to let the home screen still be visible when clicking in the widget.
How can I launch only the second activity without launching the main activity below it? I tried to remove <action android:name="android.intent.action.MAIN" /> in the main activity from the manifest file, widget works but when I install my app in the phone there's no app icon installed so I can't launch the main activity.
I hope that I have expressed my problem.
You have to finish the first Activity right after you start the second Activity
startActivity(<secondActivity>);
finish();
You can either finish your main activity after you start your second activity:
startActivity(secondActivity.class);
finish();
Or in your application manifest you can use the noHistory flag on your main activity, so it calls finish itself when you navigate away from it.
android:noHistory="true"
Perhaps having a different intent action and intent filter associated with the second activity will work. Have a look at the documentation. The notepad application example is should be similar to what want need to do.
I solved my problem like this :
1- I added to the main activity attributes in the manifest file :
android:launchMode="singleTask"
I tested it does not makes loading time when re-launching activity longer than usually because this attribute does not erase the app cache and keeps the intent instance in the memory.
2- Add this to main activity class :
#Override
void onPause()
{
super.onPause();
finish();
}
Thank you all for your help.

How to prevent custom home launcher app restart activity?

I am working on my own home launcher replacement and it works fine but one thing bothers me. When I press a home key, current home activity (the one defined in manifest as main/defualt/launcher/home) restarts - current activity instance onpause is executed and oncreate is fired again, so new activity is brought up.
On the other hand, ADW launcher and LauncherPRo does not behave like that - I do not a refresh like in my case. Launcher Pro even can do several actions:
If you are on the main screen with app icons, it zooms out to see a snapshot of all screens,
If you open a drawer and press Home, it just go back to the main screen.
Any ideas how to do that?
I just did a very simple prototype from scratch with just one activity (defined in manifest as main/defualt/launcher/home) and I see the same thing - it gets recreated if I press Home.
Add
if (!isTaskRoot()) {
finish();
return;
}
to the onCreate() of your first Activity (see Android application restarts when opened by clicking the application icon).
Add android:launchMode="singleInstance" to your <activity> element in the manifest.

Categories

Resources