I want to count my application launches and after some launches i need to request user to give feed back.So how to count launches?
Create a shared preference for your application which holds the Application launch count.
In the onCreate() of the Main Activity check value stored in the shared preference whether it is equal to the amount of launches you need if so do your operation else increment the value in the sharedpreference.
Make sure that if you use the onCreate you don't count too many starts. If you call onCreate (in stead of something like "onConfigurationChanged") when the device is changed from portrait to landscape, or if someone slides out the keyboard, then you'll get too many 'hits' on your counter!
You can find a sharedpreference example here:
http://developer.android.com/guide/topics/data/data-storage.html#pref
And check for calling the oncreate here:
http://developer.android.com/guide/topics/resources/runtime-changes.html#HandlingTheChange
Related
Ok so, there's a list of editText that the user fill with all names of players, say it's activity 2. And then click on start and play the game on activity 3. When he comes back to activity 2, the names are still in the editTexts and he can just add a participant if he wants and that's okay, but when he backs off in the menu, activity 1, and then open the activity 2 again, all the names are erased.
Is there a way to save the editText content in their places even when he leaves the activity 2 and comes back in it after? How can you achieve that?
Can you avoid calling the destroy method when back is pressed or do you need to save them somehow and put them back in?
Thanks!
If you want the names only during the application is alive, you can store the names in a Singleton class.
Or if you want the names even if the application is killed and opened again, you have to store it in a persistent storage. Either SharedPreferences or a Database (SQLite).
Check out the docs:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
https://developer.android.com/training/basics/data-storage/databases.html
According to Save data in activity's onDestroy method You should save your data in the activity's (activity 2 in your case) onStop method. Technically speaking, you could then save that wherever you wanted and pass it back to activity 2 in the onStartActivity intent.
I am creating an application that checks when the app first loads. I am creating a value in the MainActivity class (before the onCreate() method). I need this because I want something to run only on the first app load. When is the Activity class loaded? I know that the onCreate() method runs whenever the app opens and renders, but it seems that the Activity is reloading after extended time with the app closed. Is there a way to store a variable when the app first loads but doesn't reset when onCreate() runs?
Subclass Application and do whatever initialization you are doing inside of the Application's onCreate() method. You also need to specify the name of your Application class in your AndroidManifest's <application> tag. This will run once every time your app process starts.
If you want to run something the very first time the app is launched and never again (even on future launches), you should still use the Application subclass, but now you also need to store something in SharedPreferences (or elsewhere) that you can check for later to make sure you only do this task once.
SharedPreferences.Editor ed=PreferenceManager.getDefaultSharedPreferences(YourActivityName.this).edit();
ed.putString("key","value").commit();
And to get back the value from shared preferences do this:
String storedValue=PreferenceManager.getDefaultSharedPreferences(YourActivityName.this).getString("key","default Value In Case The Variable Hasn'tBeen Created");
I come with 2 questions:
First one is about Shared Preferences. I have a login page that requests data from the server everytime I do the login. I was wondering if I can store that data (most of them are URL's and Name) in Shared Preferences to load them faster if nothing changed.
I have searched about Shared Preferences and I found that it basically is an XML file stored in local path so I don't think there is a problem.
My second problem that I have searched and didn't find anything: Is there a way to hide the Activity while doing an auto login? I already implemented the auto login but I need to hide the activity and pass through the next one without the user interacting with the login one.
For the second question you can have the following flow: Launch Main Activity and check if the user is logged in. If it is, then there's nothing to do. Else, execute finish() method on actual Activity and call Login Activity. In latter you make the logic and then execute finish() and call Main Activity again.
for auto login just take one flag in preference and set it to true when you are logged In than check in your splash activity if flag is true then go to next activity else go to login activity.
I'm making this android app that acts as a calculator for a game. The calculator will tell you the cost of everything you selected and also show you a total price.
For example, if you select AR on the spinner, it will say it costs 1200. If you select WS, it shows the cost as 2400.
However, if this app is minimized for a great deal of team (30 minutes or more), the price values get reset to default (0). The spinner is still be on AR, but the price says 0. I need to click on the spinner again before it recalculates the value.
Is there a way for me to refresh it or prevent the loss of values?
It is due to that View is refreshed because when you hit the home button it will call the onPause() method then if you open another app it will then grant some memory in the others app and instead of going back to the onResume(), it will go to onCreate() due to the memory management of android.
solution:
you need to save all your data in the saveInstanceState of protected void onSaveInstanceState(Bundle outState) {
method then in Oncreate get all the saved data
You need to save your data in saveInstanceState method and then restore it in onRestoreInstanceState. After that you need to fill fields as it was in view.
Example
This is due to the nature of mobile devices having relatively limited resources.
You should save your data somewhere more durable. You might find this article on general Data Storage to be useful. This question should be relevant too: How do I save an Android application's state?
onPause in Activity - save your values in SharedPreferences or in Application (not Activity).
onResume in Activity - update select items of your spinners from this values.
You can save your data to Shared Preferences and in onResume() method you can set that value back to the respective field.
Changed text at the runtime in textview but after relaunching application retrieving default data in textview in android
In my application I have a filter function on table view which give filtered result in table. I set filtered options(checkboxes) in shared preferences so the next time the user go in filter screen it sees the checkboxes checked so it know what was the table filtered for and what he did check last time for filtering.
when the application launches i set them all to unchecked(requirement), but i dont know when the application is launched or resume. when the application is resumed from memory it again initialized all my data types. how can i check the application is resumed. if i set things in activity onResume - no gain it is called every time i just want to know only when the application is resumed.
What happens now i dont know when the application is resumed from memory, my filter behaves like application launched and set all to unchecked
what i did is, on my first activity made a boolean and put false and stored it in shared preferences, then did it true when start that filtering thing. so it remains true as the application remains in the memory and resume even. when application exits, and the launched again on startup it again turns false
There's no "resumed from memory" state. It is either resumed or created (or restarted). So you should rework your application logic and init your stuff on onCreate() instead of onResume()
I am not able to understand you question properly, but as far as my understanding goes, you are filtering user options through shared preferences. If this is so, you need not unset/set each option programatically. Android remembers and and restores this for you. If you want to access these, just call context.getSharedPreferences() and from sharedPreferences object received call getBoolean (key,defValue).
You can restore your checkboxes on onRestart(), that means your activity was not killed and is resumed after it had been stopped(either manually or by android).