I am creating an app which has a button which performs some functions on the data.
It is working well for some data but for some data when the button is pressed the button is getting stuck as in the button remains in the pressed state.
What are the reasons for a button remain in pressed state?
The only time that the button resumes from a pressed state is when all the code on the listener is run. You are most likely doing calculation intensive work in the button (in the main thread). If this is so if you click anywhere else in the application you should see an ANR (application not responding) message that closes the application.
Another reason would be that you switch the state manually and forget to cancel it on your on touch listener (if you have one)
http://www.youtube.com/watch?v=UApv-ZMJ51g
Related
On the Android dev page, it says pressing the "Home" or "Overview" button does not invoke onDestroy,
but in my app, it keeps calling onDestroy. Are there any clues?
(detail situation below)
I've built a simple app that switches from the main activity to a second activity,
but if I press the "Home" or "Overview" button on the second activity, the onDestroy gets called.
So when I go back to my app again, it shows the main activity, not the second activity.
Is this normal?
Should I save the state if I want to go back to the last activity (not the main activity) after pressing the Home or the Overview button and coming back to my app?
Android dev page that I read:
If a new activity or dialog appears in the foreground, taking focus and completely covering the activity in progress, the covered activity loses focus and enters the Stopped state. The system then, in rapid succession, calls onPause() and onStop().
and
Note: When the user taps the Overview or Home button, the system behaves as if the current activity has been completely covered.
So it is supposed to invoke only onPause and onStop, not onDestroy, isn't it?
Finally I found the culprit!
the problem was that I set android:noHistory="true" on the second activity, in the AndroidManifest.xml file.
Making this option true let the activity not leave the history,
so if another activity comes to the foreground and the user pushes the back button, the previous activity(noHistory=true) does not show up.
Similarly, if the user pushes the Home or the Overview button, then the user tries to come back to our app, the last activity(noHistory=true) does not show up either.
You have to put the Code in your Question or we can't help you.
Maybe you are calling finish() in MainActivity after you call startActivity(MainActivity.this, SecondActivity.class) ?
Use the edit-function and show us your code then we can help you more.
I want to clear shared preference when I press home button then the app is cleared swipe or simply cleared from device ram. What callback should I use ? (Like OnDestroy, but OnDestroy is not working for me)
Use onPause() for doing stuff when the activity loses focus (multi-window mode, home button pressed, swiped away, or anything that takes focus away from the activity). The rest aren't guaranteed to be called.
I'm trying to make a CountDownTimer App with some influence from the Android documentation. The App has a button and textview. When the button is clicked the countdown starts and when the same button is clicked the countdown timer stops. The timer is getting stopped when I press the back button or navigate to another activity in the App.
I want the timer to continue counting down even when the above operations are performed and the activity should be destroyed only when the timer isn't running.
How can I do this? Can anyone give me an in detailed answer?
Whenever you navigate to next activity your current one goes to onPause()(if not finished()), and the UI thread goes to sleep, so in order to update your timer when you leave and again come back to the activity is updating the UI whenever your Activity resumes itself(which is what onResume() is for). Trying using a boolean to detect pause event this is because when the activity starts it calls onResume also.
So summary is whenever leaving current activity store a boolean and while returning check it in onResume() and update Ui accordingly. Hope this helps.
I am still learning the ins and outs of Android development. I am playing around with the Notepad tutorial application to try and get different behavior.
Right now, I want to have the application do the following in the NoteEdit activity:
1) If the Back button is pressed, current state is ignored; basically, it's like an implicit cancel, and you are taken back to the list.
2) If the Home button is pressed, it takes you to the home page as normal. However, if you open the application again, it should go back into the NoteEdit activity in the same state as when you left (IE, if you were partway through an edit, for example).
I removed the "saveState" stuff from onPause, because I don't want to store to the DB unless "Confirm" is pressed (instead, I moved the call to saveState to the confirm button). By doing this, hitting "Back" basically throws out your changes, which is what I want. However, going Home and coming back also throws out your changes, though it does remain in the NoteEdit activity. Both "Back" and "Home" cause the onPause message to trigger, and both cause onResume to trigger (either from clicking on the item in the "Back" case, or by going back into the app in the "Home" case).
Is there a way to have these two events handle saving the state differently? Is it possible to have the Home button store the state (temporarily), while not having the Back button do it?
Thanks in advance!
You need to define an onSaveInstanceState method, but instead of saving to the DB (as in the Notepad sample), save your Activity's state to the Bundle. You then need to recover from the saved state in your onCreate when the passed in Bundle is non-null.
An issue has appeared a few days ago.
I have an application that listen for GPS location. The listener is a background service that works all the time, this service saves data in application level and each activity reads this data. So, when i press back button i am able to catch this event and i can stop the service, but when i press HOME button the service is still working although the application is in background mode and this consumes battery, because the GPS always works. How can i handle this event? I do want to stop all services when the user presses HOME button and start them again when user gets back.
10x
It sounds like you're catching the back button either via an onKey... method or in onStop. You should place your code in the onPause() method to ensure it's used whenever the app gets backgrounded.
You can not handle home button events in your Android application. Google has made it for internal use only.
LINK 1
LINK 2
#Override
protected void onUserLeaveHint()
{
// When user presses home page
Log.v(TAG, "Home Button Pressed");
super.onUserLeaveHint();
}
You could create a OnKeyListener, and check if the pressed key was Home. I never tried it, though.
create a custom activity called customActivity extends Activity now override method(in customActivity) to catch home button event and stop your service(create & start service in application class). Now extends customActivity instead of Activity for any activity_class.
Long press the HOME button, it will enlist the running process, select the one you want and then exit it gracefully.
OR
From Home -> settings -> application -> manage application
you can kill the unwanted process.