In splash activity I want to exit app when calling onStop() (I want to stop app if user clicks home button , Close screen or press backbutton)
Unfortunately calling finish() didn't help , even after exiting app the splash activity continue working and even starts the next activity
I would like to mention that there is an asynctask class inside splash.activity and there is also another class jsonfetch called in splash.activity ( fetch data from server and open next activity after ) I think this one doesnt stop because when exiting app it opens the next activity after few secondes
I suspect you are navigating to your MainActivity from you SplashActivity.
Try adding a global variable shouldNavigate to your SplashActivity and change it to false if onStop() is called;
private boolean shouldNavigate = true
#Override
protected void onStop() {
shouldNavigate = false;
super.onStop();
finish();
}
And then do a check when navigating to your MainActivity:
if (shouldNavigate){
// navigate to MainActivity
}
After several tests I found that System.exit(0); fixed the problem for me , I hope this solution helps anyone that has the same issue
If you are using countdowntimer in splash activity, then please stop that in onStop(). It will help you.
try this
System.exit(0);
try{
finish();
//code press backbutton
}catch(Exception e){
}
finally{ //inside
System.exit(0);
}
Related
When User click Home button app is going to background,if user open the app from background then it should go to login screen instead of last activity...please help me.
If you have multiple Activities it will be probably a pain to track "all activities are paused - go to login" or something similar.
What you can do, which will probably be a lot easier:
You have one Login Activity
Have a Main Activity with different fragments
Assuming you want some kind of "login timeout" you can track the onStop/onResume of the Main Activity like this:
#Override
protected void onPause() {
activityWasPausedOn = DateTime.now().getMillis();
super.onPause();
}
#Override
protected void onResume() {
super.onResume();
Seconds seconds = Seconds.secondsBetween(new Instant(activityWasPausedOn), new Instant());
if (seconds.getSeconds() >= 200) { //login timeout?
//go to login activity
Intent i = new Intent(....);
startActivity(...);
finish();
}
}
There is no exact callback function for android home key press. You should put logout handling code in the onPause() or onResume() methods depending upon the no of activities.
If you have only two activities, try this:
#Override
protected void onPause() {
super.onPause();
if (!isFinishing()) {
finish();
//logout();
}
}
You can override the onPause and onResume methods to achieve this.
As you mentioned that you had an issue with the back button leading back to the login screen, you can fix this by using
finishAffinity();
It is used to finish the current activity as well as all activities immediately below it. It is commonly used for logout scenarios.
You can implement the logout logic on onPause() and call finishAffinity there (Removes all activities from stack, so that hitting the back button wouldn't give you trouble), and then take the user to the login page on onResume().
I am creating an app which consists of main, register, option activities. Main and register activities should be started only once. I got it. Problem is they are again appearing when I just going back to my mobile home page without clicking exit. Even if I do not click the exit and goes back to home, they should not appear again when I entered!! Is this possible? I am not able to get the solution for this. Any one please help me.. Thankyou
you can override the method onStop() and put finish() there.
So, when the activity will be not longer visible, it will finish. Or you can put finish() after startActivity() call method.
you can do this by.
Button button15 =(Button)findViewById(R.id.button5);
button15.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
finish();
}
}
);
I want to know How an activity is visible once i press home button and relaunch the activity
for eg. If an application went in background and lauch again then MyActicity.java should be visible and not the same screen on which I quit.
Please suggest the possible solution
Thanks
Monali
As you can see in the documentation the onPause() method is called, whenever your Activity is paused. Just put this code in your Activity code:
public void onPause() {
this.finish();
}
If you do so, the Activity will be closed, whenever it is paused.
I don't know if I understood well your questions but if you have 2 activities A and B. if you are in B and press home button and then when you return to the app you want to go to A.. you have to call finish() method inside onPause() method on Activity B.
I have a custom back button that for the time being does nothing other than going to the previous activity on the back stack. Heres the code for the button :
backButton.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
System.out.println("!!! BACK !!!");
finishActivity(0);
}
});
The problem is, its not working. There is simply no change.
Can anyone kindly tell me what I have done wrong here ? Thanks.
finishActivity finishes an activity that you had previously started with startActivityForResult(), it doesn't finish the activity you are currently in - finish() does that.
Use finish() instead of finishActivity().
finishActivity() forces an activity that you started via startActivityForResult() to quit, it doesn't finish the current activity. You can also use onBackPressed(), that just calls finish() internally (see the default implementation), so it has the same effect. Useful if you override the back button behaviour though.
I want to know how to exit an app when user press the Home Button.
As far as i know that Home Button moves the running app in background and puts Launcher process in front.
I know i can use finish() but i don't know where i should call it because i have no idea which function is going to get a call when user will press the Home Key.
Thanks.
Depending on what you want to do, overriding onUserLeaveHint might be the best bet:
http://developer.android.com/reference/android/app/Activity.html#onUserLeaveHint()
This will let your app know that your app is being exited because the user chose to switch apps (Like by hitting the Home button or selecting a notification). This hint function will not be called if an incoming phone call (or similar) is causing your app to go to the background, as the user didn't initiate that action.
In my case,
I have many activities in application
so if I use 'onUserLeaveHint', 'onStop' or ...
It just catch activity transition.
So I put below code every activities.
And finally catch user's home button action.
first, you have singleton object
public class ActivityContext {
public static Context context = null;
}
and every activities,
#Override
protected void onStop(){ //onStop runs after New one's onResume
super.onStop();
if(ActivityContext.context==context) { //user home button out
AppStartEnd.sendAppEndData(context);
}
}
#Override
protected void onResume(){
super.onResume();
ActivityContext.context = YourActivity.this;
}
I hope this will help you