I want to show a Dialog or a UI when user first uses the android app. When a condition is satisfied (registration to an application server is done), the UI (or the Dialog) disappears, and a flag is set in SharedPreferences telling that android app is registered so that by each new app start the UI related to registration does not appear.
I do not know what to use since I am beginner in Android development? and which one is better from Performance perspective?
UI Fragments and show/hide fragments based on condition (check SharedPref Flag)?
Or switch between Activities ? Or is there a UI Intro that can be showed or disabled based on a Flag value?
This is pretty straightforward solution, first you just need to check if there's a flag saved in the prefs. Best to do it in onCreate of your launch activity
val settings = PreferenceManager.getDefaultSharedPreferences(context)
val shouldDisplayDialog = settings.getBoolean("first-launch", true)
if(shouldDisplayDialog) // your logic here
The second value in method getBoolean is the default one, so it will set value of shouldDisplayDialog true if there's no saved value.
After succesfull login or whatever, just write the value to SharedPrefs.
with (settings.edit()) {
putBoolean("first-launch", false)
commit()
}
That's it!
Related
I'm currently developing a widget that requires the user to be logged in. When the user initially adds the widget and they're not logged in, I take them to the login activity so that they can log in. However, I don't want to do that when the widget auto-updates (it would be very annoying to have an app randomly launch when you're just browsing your home screen).
The updating code is currently in onUpdate(), but I haven't been able to figure out how to differentiate between the update that occurs when the widget is initially added and the update that occurs periodically. Is there a way to do this?
To summarize, I'm trying to make the following:
- Initial update when user adds the widget: Open login activity
- Subsequent periodic updates: Don't open login activity
Note: I'd like to avoid onEnabled(), since that is only called when the FIRST widget is added. I'd like my code to run every time a new widget is added.
If you are looking for just one instance of your widget, then you could go with shared preference boolean to solve this issue.
onEnabled - Clear boolean
onUpdate - if boolean not set -> Means first time (Do your work and set the boolean)
If boolean is set -> Means its the normal widget update.
Work around option if you need to deal with multiple widget instances :
If you need to achieve the above requirement, you need to handle auto update in intervals by your own.
That means, all the the call towards onUpdate should come from your own created Intents. That is :
Use Alarm manager to trigger the onUpdate functionality of Widget. Add bundle value to intent stating its an update call.
All widget interaction intents should contain the bundle value to say its an update call.
In onUpdate method, check for the same bundle value mentioned above and if its there, its a normal update else BINGO... :)
My application sometimes resets the UI into the initial UI state just like after the first initialization.
The event is occurred when I use another application while the problematic application is sent to background / is sleeping. Then when I change back to the problematic application, it displays the initial state of UI (The initial state of the main page).
I suppose I need to save its state before I minimize the application on OnSleep() event and restore it back on OnResume() event.
How can I do this?
I have solved the problem by using global variable to store whether the application is still running or not.
public MainPage()
{
InitializeComponent();
if(Config.Config._RunningGetLocationThread == true)
{
lbl_Title.Text = "Tracker is running...";
btn_StartTracking.Text = "Stop Tracking!";
}
else
{
btn_StartTracking.Text = "Start Tracking!";
lbl_Title.Text = "Tracker is not running";
}
}
Config.Config._RunningGetLocationThread == true is the global variable and set to true when the application is first started.
I think there should be a better way to save the state of the UI elements. If anyone know the best way to save state UI elements, please share here.
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).
I'm looking to figure out a solution for helping a user enter some configuration data during the first time it's executed. I have a main activity that shows a list of photos in a grid.
During the onCreate method for the main activity, if I find that a user has not configured the app before, I switch to a series of two activities. The user fills out the required data in those two activities (think wizard-style, one after another). Once they have entered data on the second activity, they are shown the original main activity.
Is this a standard, or preferred way to do things? I couldn't find an easier way to do this, or a built-in way.
Instead of using onCreate, is there a way to programatically choose the first activity based on whether it's the first time the app has been run or not?
Thanks,
Kevin
use below code for this problem
if( getSharedPreferences("FirstTime", 0).getBoolean("check", true))
{
getSharedPreferences("FirstTime", 0) .edit().putBoolean("check", false);
/////////Write First Time code
}
else
{
/////Write Second Time code
}
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