My app closes when screen turns off [closed] - android

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
I got a problem with my app when the screen turns off (because of system screen timeout) my application finishes.
I search but didn't find something helpful.
Is it a common problem or does it have a fix?

you shud not call finish() in onPause. it could be called for variety of reasons(check doc.). why you want to kill your activity when user switches app? its not recommended.
here are some posts, but there is no api available to detect app going in background.
How to detect when an Android app goes to the background and come back to the foreground
http://nathanael.hevenet.com/android-dev-detecting-when-your-app-is-in-the-background-across-activities/

I want when the user leaves this activity to finish but not the screen thnx
Take finish() out of onPause(). Put it wherever the user leaves the Activity. So, assuming you have code that starts a new Activity, put finish() after startActivity().
You also could use the flag android:noHistory in your <activity> tag of your maifest.xml so that the Activity is removed from the stack whenever it starts a new Activity. Both of these methods do the same job it just depends on how you want/need to implement it.
The reason it closes when the screen turns off is because your app calls onPause() at that time so removing the call from that method would keep it from closing when the screen turns off.

Related

app corrupts when onTouch is triggered while changing activity [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 5 years ago.
Improve this question
I made a little game touch the screen to move up, release to go down. However, the player can die by colliding with the screen bounds. On collision, a new activity starts. 'Unfortunately the app has stopped' appears when I keep pressing after the collision. Using onTouchEvent.
What can I do?
I am using Eclipse, and appreciate every answer
Take a look at the code that you execute in response to the onTouch event. Can it execute if the Activity is being destroyed or has already been destroyed , e.g. are you using a background thread or an AsyncTask? You need to respect the Activity lifecycle, otherwise you may be attempting to access an Activity that is no longer in an active state.

How to make intro activity appering only once? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I just created intro sample show some tips in my app with 3 fragments and it works well but it only appears to user when I made the intro activity in manifest as "launcher" activity. So it appears every time the user opens the app.
What I need is how to make this activity appears only the first time opening the app?
You can use SharedPreferences and set value in that when User will open it for the first time. after that Every time user open the App you can check that variable. If that value is set then you can directly call next Activity.
But for this approach you have to make that <activity> with attribute android:noHistory="true". then It will work.
You can set an activity before your intro and using SharedPrefereces you can store when user open your app for the first time. In the case that is the first time you start the example activity else you start the main activity.
You can save just a boolean value SharedPreferences

How to check if application exited or closed android? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I want to perform some activities like clearing cache, singletons clearing etc. I will write all this code in Service which will be background but I need this service to be triggered only when application is closed or exited.
I cant put my code to statrt service in onDestroy of activity as whenever i will move between my activities it will call onDestroy of previous activity.
How to know if application is exited.
Example will really help.
Thanks.
The ActivityManager should be what you are looking for.
For example see here
in your main activity, capture the "back" key and check for Activity.isTaskRoot() whether it is the last one. If yes, your app is going to "exit", then do some communication with your service.
please note pressing the Home key doesn't mean to "exit" your app

I need to start an Activity immediately. how? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
in a part of my app, I need to start an Activity immediately, I mean it should immediately comes up and fills the screen but no matter if it takes some seconds to load the view and widgets(and it has no I/O operations). is there any specific thing in Android for such purpose?
for example base on this article:
On iOS, the system displays the launch image instantly when the user starts your application and until the app is fully ready to use.
that is about launch time, however. but I want to gain something like that.
I know I should optimize the onCreate method and avoid time-consuming operations in the UI thread but I'm searching for another specific way of staring an Activity immediately.
Sounds like the flow of code is causing you a problem. I would suggest putting nothing in the onCreate(). And the layout that has all of your elements set them all to gone. That way when the activity starts, you're given a blank black screen. Then turn all of your layout to visible via code and any of your start up task that were originally in the onCreate() can now be launched. You can use a tree observer to determine when the layout has filled the screen to fire something, like a method call or async task for example, to finish all of your loading.

How to kill an activity from a different class? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Being relatively new to Android, I guess I didn't word my question properly. Here is what I am trying to do. Generally when you start a new intent, you finish the old activity by using class.this.finish() . But I want to finish a different class, not the current class. Let's say I have an activity with a button. Clicking the button would take me to a second activity. Now clicking another button takes me to a third activity and simultaneously finishes the first activity. Probably finish is the keyword here and not kill.
You could have a BroadcastReceiver registered in first activity, and send a broadcast from the third activity. Then in onReceive() method of the receiver finish() the activity.
However, re-thinking the design could be a better solution.
This is a question that doesn't have a straightforward answer. Killing activities is not a good design pattern for Android, so my first thought is "don't do this." Of course, that's not very helpful.
We may be able to help if you describe what you're trying to do. We may be able to suggest an alternative that doesn't requiring killing an Activity, or allows one Activity to finish another in a more "approved" manner.
Sometimes the answer to a problem is to suggest a different problem. Like the robot banging against the wall, the problem is not cutting through the wall, but learning how to turn and go on.

Categories

Resources