I am using a Full-Screen activity in my app in android studio, and everything works fine, but when the splash screen is over (after 3000 ms), the main activity or home starts, but if I use the back button from the android phone, the display goes back to the splash screen and it stays on this splash screen (you can not go back to the main activity again)...
What code can I use to avoid this? (only show once the splash screen when the app is launched and never go back to it even if I press the back button).
Thanks in advance..
You need to clear the back stack when you go from the splash screen activity by setting the FLAG_ACTIVITY_CLEAR_TOP & FLAG_ACTIVITY_NEW_TASK to the intent.
So, in the splash screen activity:
Intent intent = new Intent(this, HomeActivity.class); // or MainActivity
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
finish();
I found the answer to this. If you want to prevent the MainActivity from going back to the splash screen, you can use the next method in the Main Activity:
#Override
public void onBackPressed() {
moveTaskToBack(false);
}
This method will also prevent the user from getting out of the app if they press the back button (so they stay on the app until they press the home button or the other button).
Related
I have an activity which has a button, upon clicking that button the screen changes to a empty screen with just a progress circle. Then after 30 seconds it should show the next screen, but before this screen it is showing first activiy for a second and then switching to this one.
For Example:
Screen1-> Screen2-> Screen3-> Screen4
When moving from screen3 to screen4, screen2 is displayed for a second. How do i prevent that?
Also I need to be able to go back to screen2 from screen4 on back press.
I searched many similar questions on SO,but none seem to work for me, for example. Giving a finish() call in screen2, the application does not even start screen2 stays in screen1.
Also tried setVisible(false), this does not work in case I click back button in screen4, it displays blank screen instead of screen2.
My apologies if I did not make the question clear, Screen2 is an activity which starts a service while Screen3 is active and then stops the service when the app moves to Screen4.
I got this solution:
registerReceiver(finishCallReceiver,new IntentFilter("Done"));
finishCallReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
finish();
}
};
Then in the service I just call
Intent intent = new Intent("Done");
sendBroadcast(intent);
Also register the Broadcast Receiver in onResume of the Activity.
I know this question has been answered in another posts also. But it doesn't solve my issue.
I have 3 screens: A,B and Home. App starts with A, then goes to B and then to Home page. But I want to delete activities A and B when it reaches Home activity, so that when back is pressed app exits.
I tried:
Intent.FLAG_ACTIVITY_CLEAR_TOP. It works fine when back is pressed. App quits. But if I again open the app from background, it starts with screen B.
Intent.FLAG_ACTIVITY_NEW_TASK. This works exactly as I want. But when Home activity starts, there is a sudden glitch in the screen and it is not smooth.
Intent intent = new Intent(getApplicationContext(), Home.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
This will clear all the activities on top of home.
Try the following code:
Intent i = new Intent(YourActivity.this, HomeActivity.class);
i2.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
finish();
startActivity(i);
You can do this way also, for your splashScreen(A) you can set nohistory=true in AndroidMenifest.xml, and goto Activity(B), and when you are going on Activity Home from Activity(B) finish activity(B) than goto Home activity.
so both of your previous activities will not store in stack.
And on Activity B override onBackPresh() method but remove super.backpresh() in side it. and start your activity A with finish this current activity, it will work.
I have 4 Activities.Splash Screen -> Login Screen -> Home Screen -> User Screen. When I am in My Application's Home Screen I had pressed Device HOME Button at that time application going to background. If I open the application again it shows from Application's home screen. Now I need to show Splash then Login Screen for every time.I have called finish() for every Intent calls. How to do this ?
In HomeScreen Activity, write this in onResume()-
Intent intent = new Intent(getApplicationContext(), Spalsh.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
In your manifest file, in the Splash Screen activity (which I assume to be the root activity) add the following line
android:clearTaskOnLaunch="true"
Use this for the activity in your AndroidManifest.xml android:launchMode="singleInstance"
When i click training icon it show the button named Home.
When i click home button it shows the home screen as below.
My application is currently running right, I want to close the application and have to show the home screen when i click the app icon(training).How could i do this..?
Actually i have a button in my layout and i'm showing the home screen with the following code when the button is pressed.
Intent startMain = new Intent(Intent.ACTION_MAIN);
startMain.addCategory(Intent.CATEGORY_HOME);
startMain.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(startMain);
It shows the home screen and my service in background has started.
I want to stop all the action from my app to be stopped when i click launcher icon.
My question is how can i stop the activity when my application icon is clicked again.
Sorry for poor english..!
If you application consists only of an Activity then it is already stopped when the user re-clicks the launcher icon (since he had to exit the app).
We need more info if that's not your scenario.
Use it in your activity. When you exit from your app it will stop the action as well as destroy the app. And it will launch new activity when you click your app icon again.
#Override
protected void onDestroy() {
android.os.Process.killProcess(android.os.Process.myPid());
}
Maybe you can check if your service is running, or more dirty with a static int counter. Let the counter increment in your onCreate(). Now you can check for the second start of your apk
Put finish(); on the method onResume().
Of course, if you need to do something else, just put your code before the finish();.
It should do.
In my application, the first activity takes care of the splash screen/loader and onces it completes the load, a second activity comes up or starts.. Now when a person clicks on back or navigates back, it is supposed to exit the application but it goes tthe loader or splash activity..how can i avoid this ?? Hope my question was clear...
When your application opens second screen aka second activity finish the first activity aka splash screen.
Just use finish() method to achieve this.
Here is an example
Intent intent = new Intent();
intent.setClass(context, Second.class);
startActivity(intent );
//call finish
finish();
when you start second activity after then CurrentActivity.finish() current activity in your case like splash...