Avoid splash screen activity when pressing Back button - java

In my application, I have two activities. First is a splash screen, which simply shows the application name and few other info.
Upon clicking on the splash screen activity, I'm loading the main activity. My app works fine, but I'm facing a small issue. If I press back button from my main activity, control is going to splash screen activity. But I don't want to show the splash screen activity again, I want to avoid splash screen activity when pressing Back button.
Is it possible? If so how?

In your AndroidManifest.xml file, add android:noHistory="true" attribute in your splash screen <activity>.

As I understand, you want the splash activity to not show after changing activity. You should note activities save On Stack and with starting new activity push on it and with finish you pop on top stack. I think that if you the call finish() method your problem fix as in your splash screen activity where you call StartActivity insert finish() after
public void onClick(View v) {
Intent intent = new Intent(Main.this, Splash.class);
startActivity(intent);
finish();
}
Hope to be useful :)

You can just call
finish();
In your Splash screen when you jump to the second screen.

In addition to the above answers, you should note
that:
1: by calling the finish() method, the Splash activity
will close after execution, meaning that it will not be
available in the stack.
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
You will achieve your aim using the method above
but...
2: If you want to prevent your users from force
exiting the app (pressing back button) while the
splash activity in still ongoing which is the best
practice in android, then you need to call the
onBackPressed () method.
Class NoBackSplash{
#Override
protected void onCreate(Bundle saveInstsnceState){
super.onCreate( saveInstanceState);
\\ do something here
Intent intentSplash = new Intent(SplashActivity.this, NextActivity.class);
StartActivity(intentSplash);
finish ();
}
#Override
public void OnBackPressed(){
};
}
With this OnBackPressed() method, your splash activity will not be force to exit no matter how hard the user try.

I understand, you want the splash activity to not show when you click on back button. First of all you should know that all the activities on android are in form of STACK. So what we need we just end the splash activity after it execute. we can do this by calling finish() method in android studio. here is the solution:
Intent intent = new Intent(MainActivity.this, home.class);
startActivity(intent);
finish();

public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
Handler hd = new Handler();
hd.postDelayed(new Runnable() {
#Override
public void run() {
Intent i = new Intent(Splash.this,MainActivity.class);
startActivity(i);
#by calling finish() method,splash activity will close after execution
finish();
}
},3000);
}}

Related

Avoid Splash Screen to launch when user open app from recent screen

I wanna know how I can avoid splash screen to launch when my app is in recents.
I want the app to open the activity where user was in it before when he/she clicks back button of device and opens
app from recent screen.
This is a splash screen activity :
public class MainActivity extends AppCompatActivity {
//variables
private static int SPLASH_SCREEN = 1000;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
Intent intent = new Intent(MainActivity.this , Main_Page.class);
startActivity(intent);
finish();
}
},SPLASH_SCREEN);
}
In the manifest, specify for the activity:
android:noHistory="true"
I found the solution to my problem.
This happens because when I click back button of device, according to
activity lifecycle , ondestroyview() will be called. so when
I returned from recents screen , app started again.
what I've done is that in the next activity which appears after Splash
Screen , I used onBackPressed like this :
#Override
public void onBackPressed() {
moveTaskToBack(true);
}

how to close main activity after closing another activity in android?

I am developing an android application. So in there I have button called "Aboutus". when I click that one it starts another activity and show corresponding view.
Here is the code for button Aboutus click event.
aboutus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this,AboutUs.class);
startActivity(intent);
}
});
}
And then in that aboutus activity I have back button.when we press that back button it will go to the main activity again.
back button key event goes like this..
back.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
startActivity(intent);
finish();
System.exit(0);
}
});
here i put some intent extras for some other purposes.In this case they do not matter. So and then again in mainactivity i have exit button. which should kill whole app.
and my exit button code goes like this.
exit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
finish();
System.exit(0);
}
});
Exit button works perfectly (it ends the app) except for one scenario. if we click on aboutus and go to that activity and then press back button on that aboutus activity and then again come back to main activity and finally when i want to exit it won't kill whole app, instead it again goes to the about activity.
in conclusion,
MainActivity---> click aboutus button(no problem in here.this will start aboutus activity)
Aboutus----->click back button(this also works fine. go back to main activity)
MainActivity--> Exit button (not working .it goes to aboutus activity again)
So how to fix this problem?
i dont see why you have to start MainActivity.java ...the user can just hit the back button as long as its on the activity stack. Also The system.exit(0) is bad. Just let the app finish gracefully with finish().
if you want to pass something to the activity when aboutUS is done, you can look up onNewIntent() method.
use intent flag ACTIVITY_CLEAR_TOP to clear all activities on top of it:
Intent intent = new Intent(AboutUs.this,MainActivity.class);
intent.putExtra("aboutus", true);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(intent);
FLAG_ACTIVITY_CLEAR_TOP From the doc:
If set, and the activity being launched is already running in the
current task, then instead of launching a new instance of that
activity, all of the other activities on top of it will be closed and
this Intent will be delivered to the (now on top) old activity as a
new Intent.

Making an activity which started itself go back to previous screen with "onBackPressed()"

I have an activity which is repeated "x" times with this piece of code :
//create intent to start itself again with different parameters
Intent intent = getIntent();
intent.putExtra(CATEGORY_ID, temp.getCat_ID());
intent.putExtra(CATEGORY_SHOPID, temp.getShopID());
intent.putExtra(CATEGORY_SITEID, temp.getSiteID());
intent.putExtra(CATEGORY_NAME, temp.getCat_Name());
finish();
startActivity(intent);
My goal is that when the user presses the back button that the same activity gets started as there was before with the same parameters as before and such. I tried saving them in an Activity ArrayList but that doesn't seem to work.
When you call finish, your activity will end.
When you don't call finish, your activity just go to the background. In that case, when you press the backbutton in your new activity, your previous activity comes up.
I think when you just delete finish(), it will work.
It's okay if you save the parameters you've already set earlier, but you need to retrieve all that information from the Intent itself and repopulate your activity with the proper data.
This must be done like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String categoryId = getIntent().getStringExtra(CATEGORY_ID);
// retrieve the others and populate your activity
}
in your activity, get event back
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK ) {
// and you have to make a intent to call this activity again with parameter other
finish(); // and call finish() here to close the old activity
}
return super.onKeyDown(keyCode, event);
}
hope this help!
Have you tried overriding the onBackPressed method?

How to Close an Intent Activity from another Class

Basically I'm using the following code to open Google Navigation within an activity
String uri = "google.navigation:ll="+LAT+","+LNG;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
Where LAT & LNG are the coordinates of a specific destination. Basically what I'm trying to do is once the destination is reached, an action would be triggered where the navigation activity closes and return to the same Class it was opened in.
Any help would be much appreciated.
EDIT:
i guess since the activity i opened "Google Navigation" is beyond my control it is impossible to close it from within the app. So the best solution would be to reopen the current activity once the destination is reached.
Appreciate all the answers.
Use finish() method to end the first activity
use Finish() method to kill your activity as
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
finish();
Use finish() method to end an activity
Use finish() to kill the activity.
Add Finish() method to end up an activity#
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
finish();
If you press your back button it will work !! don't finish() the previous activity.
Dont call finish() when you are starting the intent. Once you finish with navigation press back button
Maybe you should put a thread to compare the destination and current location.
Like this:
Thread thread = new Thread(){
#Override
public void run() {
// TODO: Do your work here
myHandler.sendMessage(new Message());
}
};
create a handler and implementing the handleMessage method.
Handler myHandler = new Handler(){
#Override
public void handleMessage(Message msg) {
// TODO: finish the activity here
finish();
}
};

switch between activity with rotation animation

does anybody knows how to launch a new activity with rotation animation?
I'll try to explain what i want to do:
For instance i've looked for android app exemple in skd sample "apidemos" and i've found a class named com.exemple.android.apis.animation.Rotate3dAnimation.java and com.exemple.android.apis.animation.Transition3d.java. These classes allow me to switch between image with rotation effect.
That I would like to know if there is a way to do the same but instead of image, i will be activity (whith new layout).
The window manager doesn't support 3d transformations at this point; since each activity is a window, animations between activities are window animations, so they are limited to what the window manager supports.
This is how we can accomplish this.
Suppose we want to switch from Activity A to B. First we will animate activity A then we will start activity B in overridden function "onAnimationFinished". This will ensure that activity B is started only after animation for activity A has finished off.
// we will only animate activity A here.
// The activity B will be animated from its onResume() - be sure to implement it.
final Intent intent = new Intent(getApplicationContext(), B.class);
// disable default animation for new intent
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
//Animate A
ActivitySwitcher.animationOut(findViewById(R.id.A), getWindowManager(), new ActivitySwitcher.AnimationFinishedListener() {
#Override
public void onAnimationFinished() {
// Start activity B
startActivity(intent);
}
});
Now override "onResume" function for activity B
#Override
protected void onResume() {
// animateIn this activity
ActivitySwitcher.animationIn(findViewById(R.id.help_top), getWindowManager());
super.onResume();
}
You can see here for working example
http://blog.robert-heim.de/karriere/android-startactivity-rotate-3d-animation-activityswitcher/comment-page-1/#comment-12025

Categories