back button's function on first activity
back button's function on first activity
Normally when the back button is clicked it goes to the previous activity and if the current activity is the first activity, the application closes.
I have a splash screen (that is logically my first activity) and then the menu activity loads.
I want to close the program when the back button is pressed on my menu activity (as if it is the first activity) and avoid going back to the splash screen again, but I know that I should not exit the program.
I was wondering what is the functionality of back button on the first activity?
does it put the program to pause?
@Nuwan94 best solution +1....but I suggest to add a view group contains the splash screen and show it in his menu activity
using Handler
for some time then hiding it– Abdulmalek Dery
Jun 30 at 14:38
using Handler
@Ashkan Add finish() code in splash screen activity after start firstactivity code.
– Mohamed Mohaideen AH
Jun 30 at 14:54
4 Answers
4
Avoid splash screens !
Instead of using your splash as the main activity, use menu activity as the main one, and in onCreate()
chain off the splash activity, which will close and disappear forever.
onCreate()
When new Activity is launched from first Activity, first Activity is executed until onStop() method, then it stops and waits for relaunch, unless you killed it by calling .finish(), in that case launched Activity becomes first Activity and back button will minimize application on back button press. In order to control what application does on back button press you can override this method in your Activities and implement your own custom behaviour:
@Override
public void onBackPressed() {
super.onBackPressed()
}
While you are on the SplashScreen, on the method calling your menu activity I assume you are doing something like startActivity (new Intent (this, MenuActivity.class));
in Java
or startActivity(Intent(this, MenuActivity::class.java))
for Kotlin
just after that call finish()
this will remove your SplashScreen from the back stack
startActivity (new Intent (this, MenuActivity.class));
Java
startActivity(Intent(this, MenuActivity::class.java))
Kotlin
finish()
add this attribute to your splash activity (in manifest):
android:excludeFromRecents="true"
android:excludeFromRecents="true"
pressing back in per activity, transfer control to prev activity in stack (back stack) that not excluded from "Recents".
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
try this stackoverflow.com/questions/3625394/…
– Nuwan94
Jun 30 at 14:34