android klling an activity maybe prevent me from force close - android

I am having an error state in my app , so when the user doesn't have internet enabled view an xml to inform that he should connect first. The problem is that when he enables internet and tries to connect he might gets a force close. I do not know why is this but I think that if in my error state screen add code for killing the activity on exiting will help me solve this. My question is rather simple. Do I need both of them? Or only of them? Add anything else?
#Override
protected void onStop() {
super.onStop();
// The activity is no longer visible (it is now "stopped")
finish();
System.exit(0);
}
#Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
finish();
System.exit(0);
}
The flow of my app is this: user enters the app, check if is online. If yes go to the main screen and everything goes according to the plan. If now go to the error state. So, if called, the error state will be the first activity to run (after the launching one).
EDIT: I just want to inform user that there is no connection, so please try again and because of this kill all the activities running (This is the only one actually as if it runs it will be the first). So next time he enters the app, start from the beginning not from that point that he was earlier.

That depends. OnStop and OnDestroy have two different purposes. You should surround what ever it is that may error with a try/catch to avoid fc
#pseudo code
Try:
Make a connection
Catch
Dialog to alert that there is no connection
super.finish ()

Never use
System.exit(0);
Let the main activity launch finished, then check connection. If there is connection, everything is fine. If not, pop up an AlertDialog which call finish() onClick.

Is there any problem in finishing main activity when there is no internet connection? and also when the main activity get finish , after re launching it will start from beginning.
well refer this thread :
How to close Android application?

Related

close application when minimized

On start my app displays a splash screen and checks via network if the current user is still premium.
My problem: I started my app right before I went to bed and minimized it by pressing the home button. In the morning I launched the app again and it resumed the activity from the night. The app never really quit, my splash screen was not shown and and it couldn't check if the user is still premium.
So how can I achieve my app to be closed after a certain time (e.g. when the app is minimized)?
You should write the Premium user check logic in your onResume() method so that
if the activity is in pause or background state it will check the
logic every time it will be launched .
Don't try to finish app when it's minimized. Use Activity lifecycle callbacks.
#Override
protected void onResume (){
//check for changes here
}
If you want to end an activity, you call finish(). So you could record the time in onPause, then in onResume, check how long its been. If its been too long, call startActivity on your main activity, then call finish() to end the old one.
I think you should become more familiar with the Android Activity Lifecycle and think about which call back in your activity should you check if the user is premium

How to check whether an application is being closed in android

I am trying to mantain a log , when exits the applicaiton. I have used this code :
public void onDestroy() {
super.onDestroy();
Log.d("D", "Destroyed");
}
But this only works when I press the Back button. When I press Home button , the application Pauses , and If I close this application from task manager , then the onDestroy function is not called. How to handle this ?
Any idea ?
You can't handle the closing of application from task manager. In this case you're killing the app and onDestroy isn't called. You should make all clean up in onPause
You can do your stuff in onPause() method.
In your case:
If End Process is used from Process list in task manager, then nothing is called in application, the application is simply terminated.
If End Task is used from Applications list, then WM_CLOSE is sent to the window, which in turn allows application to do the cleanup.
onDestroy() is called when an activity finishes its life cycle. It is also called once in the lifecycle of an activity.
The OS decides when things "go away." The onDestroy is there to let your app have a final chance to clean things up before the activity does get destroyed.
From the Android Developer Guide:
There are a few scenarios in which your activity is destroyed due to
normal app behavior, such as when the user presses the Back button or
your activity signals its own destruction by calling finish(). The
system may also destroy your activity if it's currently stopped and
hasn't been used in a long time or the foreground activity requires
more resources so the system must shut down background processes to
recover memory.
When you switch between apps by pressing the home button, Android pauses the activity and resumes it when you return to the activity.
For the most part, the OS decides when to quit an application so it wouldn't make sense for you to log when an activity is destroyed. I would suggest overriding the onPause() or the onStop() method

Thread Continues to Execute After Closing the Application

I have an android application that uses Threads. Application waits for some time, then executes a function.
Things go pretty well if user waits for some time. After the predefined time t ends, the function gets executed.
However, if the user clicks on back button of the device and return to main screen, after the time t ends, the application appears again.
How can I understand if the user pressed back, or closed my application? How can I stop the thread and release everything if I get the leaving message -let's say USER_EXITED?
in your activity when the activity is going to end you can check if it is finishing like this and take care of things to do with your threads
#Override
public void onPause() {
if(isFinishing()){
//put the correct checks or shutdowns
{
super.onPause();
}
I think that you need to create a Service.
http://developer.android.com/reference/android/app/Service.html

Stop application from running in background?

How to stop application from running in background in android?
I want my application to start fresh everytime it loads. How to do it programatically.
Override onStop method of your activity:
#Override
public void onStop(){
super.onStop();
finish();
}
But I think it's a bad idea to restart your app each time. It's better to override onStart method, and handle "restart" here.
Actually, your app doesn't "run" in background. Android OS keeps it in memory, or saves state of your activity to device (and then you can load it, using savedInstanceState param in onCreate method).
You can use onResume event to reload again, or look here.
EDIT:
Actually you need to use these functions to reload your application when user navigate it.
After adding finish();
This code will completely stop the application.
System.runFinalizersOnExit(true);
System.exit(0);
android.os.Process.killProcess(android.os.Process.myPid());
The whole Android ecosystem is based on the fact that the user shouldn't have to worry about "terminating" or "starting from scratch" an application. If you need to start your application from scratch every time, that's probably because you have tasks in your "scratch" that shouldn;t be there, and should probably be somewhere in onResume.
Please give us more details if you want a more detailed answer.
you should make use of Intent.FLAG_ACTIVITY_CLEAR_TOP to finish all other activities running in activity pool and call the first activity where you can ask to exit from app

Killing android application on pause

I have an application which I would like to be fully disabled/closed when it is paused (IE. When the user presses the Home, End (call) and Back button I would like the application to be closed, instead of being saved in the history stack).
How do I do this....?
Thanks.
Implement onPause() in your activity and call finish() on your activity. Bear in mind, though, that this will occur on every pause, including dialogs, incoming calls, users activating a Notification. You might want to consider doing finish() in onStop(), which would at least solve the dialog problem.
Also, bear in mind that users will may get confused when using your app, thinking it has crashed since it is gone when they try to get back to it.
you can easily do that by setting true the "noHistory" attribute in to your activity element, in the manifest
http://developer.android.com/guide/topics/manifest/activity-element.html#nohist
You know how you have an OnCreate() method in your activity which performs actions when you start. You need to add something like:
#Override
protected void onPause(){
finish();
super.onPause();
}
in your activity to add actions before it starts
in this case the
finish();
command is what you want to execute before your activity pauses.

Categories

Resources