i have two simple questions (hope so).
First i want to close my app, when the user hits the homebutton in my HomeFragment, because there is a counter (Days,Hours) and when I click the home Button and come back later there is still the old value.
Then i have to close the app via backbutton and start again. So i want my app to get killed, when I hit the home button.
Then i have a button to delete the last value, on which the counter is based. When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
When I hit the button i want to run my homeFragment again. Should i just create a funnction which calls, the homefragment.onCreate again?
No, these functions are meant to be called by the system.
You are probably looking for the onStop() method (also called by the system)
protected void onStop()
{
super.onStop();
// clear counter e.g.
myCounter.setText("");
}
Other than this, it's kind of hard to tell what you want. Feel free to add more (if not all) of your code.
Related
I'm making an Android app but I keep running into a odd bug. After the game, if you tap the 'Back' button on your phone, you end up back at the finished game and you can keep playing!!
How do I prevent this from happening? Is there a way to delete the previous SavedInstanceState? If so how?
If you really don't want to restore the data don't pass them to your super class. However I would prefer to reset the game data myself.
I guess the full solution is to call finish();. I guess you have this activity stack: Menu -> Game -> End summary. So when you start your "game over" activity add a finish() call to close your current game. So a back button press let the user go back to your menu.
I'd like to do certain, simple action every time that my app is closing - I just want to decrease int variable. How to do that?
As #Ozair has rightly asked that it depends what do you define as closing of app.
You can do either or both of the following depending on the need:
If you want to detect closing of app by use of BACK button, then from within your last activity, you can detect the pressing of BACK button by overriding onBackPressed function. There you can decrement your value.
If you are also considering the situation when you app goes into the background by pressing of HOME button, then in your activities you would have to detect the HOME button pressed. There have been many solutions which no more work for detecting HOME button but this answer on How can I detect user pressing HOME key in my activity? question seems to work for me.
So, there you can detect the HOME button and decrement the value which you can save in SharedPreferece.
There can be other cases where you are calling finish() and closing your last activity. It is not clear from your question if you are considering that case as well.
Hope this gives you some opportunity to think about it.
The question is what you mean "close"?
If you close all your Activities, the App-process might still be running. If you mean that the "close" is just closing all of your Activities. You might define a "count" for all opening Activities, you can store it in DB or SharePerference. I think you can do follow(dummy codes):
In your project, you should define BasicActivity:
public class BasicActivity extends Activity {
onCreate() {
mPreference.incActivityCount();//++
super.onCreate();
}
onDestory() {
mPreference.decActivityCount();//--
if( mPreference.getActivity() == 0 ) {
//All being opened Activities have been closed.
onAppHasNoUIs();
}
super.onDestory();
}
onAppHasNoUIs() {
//All being opened Activities have been closed.
}
}
How to complete the application when pressed buttons Home and Back? Provided that in my memory holds many pictures.
When pressed on the Back button - application restarts... When pressed on the Home button - quit application, but when it restart - does not start Splashstsreen.
Hard to see without code but it sounds like your activity is resuming rather then starting from scratch (as it should behave). It sounds like it's behaving correctly in that case. If you insist in wanting your application to truly quit after the back button is clicked perhaps you can override onBackPressed() then have your Activity call its finish() method.
I don't think it's good programming practice to fiddle and interfering with the activities lifecycle. It's the OS responsibility to manage the lifecycle, including pause and finish activities.
Instead you should use other methods to handle your problem with not showing splash screen, these methods are onResume and maybe also onStart(). Also you should get familiar with the activity lifecycle(link submitted by #ss1271).
Press Home will let the activity to enter onPause(). however, if you insist to quit the application when press HOME, which is obviously not a good practise, you can do like this:
Override onPause() and onBackPressed()
add finish(); to these methods.
I am sure by adding finish(); to onBackPressed() will quit the app when Back button pressed, but I haven't tried on Home.
Please refer to Activity Lifecycle for more info.
if a user tapped the home button and open the app after that, how to not allow back? eg don't allow users to go back to screens that they seen before tapping the home button. It should be treated as a new session
This sounds like a bad idea, as it blatantly goes against the Android task/navigation guidelines.
The user expects to be able to back out to the previous screen after resuming a task... and preventing it will potentially piss off a lot of users.
Please, please, please read these documents before you risk destroying the user experience.
App structure
Navigation
Tasks and back stack
The home button cannot be overridden nore should it, if you dont want the user to go back to the activity they left when the home button was clicked then on the on pause of the activity just pop the backstack to where you want to be.
see this answer
If you want to end your Activity once it is no longer visible then finish your activity in your Activities call to onStop().
#Override
protected void onStop() {
super.onStop();
this.finish();
}
This will finish your Activity with no chance of onRestart() being called. Be careful with this method because users expect to resume the app instead of having to start over, but there are cases where this would be the accepted protocol. See more on Navigation and the Activity LifeCycle.
Edit:
Also see the question Android-Quittings an application - is that frowned upon? specifically this answer.
Im connected to a chat server which gives me messages i print out in a textview. This continues when the user leaves the application by pressing the home key etc etc. I would like to close all streams if the user goes back with the phones back button to the previous activity. Problem is that onStop() and onPause() are both called independent of if the Home key was pressed or the back key. Its just called when the activity loses focus or visibility, doesnt matter which way it happened.
How do i find out if the back key was pressed, and not home?
You can implement your own version of onBackPressed(). That way you'll know every time it's pressed and can do what ever you need to in that callback. So do something like this:
public void onBackPressed(){
//Do the stuff you want to do
//Then call the parent class version function to allow it to do any other stuff
// that needs to happen as well.
super.onBackPressed();
}