Hi guys I have been looking around for the answer to this but have come up with nothing, I would like to have an activity that asks the users for their details and then send them details to an external database after they first install the app. Does anyone have any suggestions on how to create an activity that will only be used when the app is launched for the 1st time. Cheers to anyone who can help
Save a flag in shared preferences that you set after completing the activity. You can actually save this flag anywhere, as long as you have it in some sort of persistent storage. Then on load, check in that activity if the flag exists (you can do this in onCreate), if it does, launch an intent to another activity.
Create a file or store a persistent property at the first boot.
Check for the existence of that file/property at every boot.
Related
How do I manage a session in an Android app after the user successfully logs in? Should I use SharedPreferences or static variables to store the session info?
The user must log in every time the app launches too.
Try to use Account Manager. It was designed for it. Here is a tutorial for it.
Anyway it is not recomended to use preferences for it, because at the end it is plain XML file, so anyone can steal data from it. Also it is not recomended to use static variables, because you never know when your process will be killed. For example somebody could call to your phone and android could kill your app and restart it after call ends.
I have an unity activity, which launches in another process than my App:
<activity
android:name=".UnityActivity"
android:process=":UnityKillsMe"/>
I save my SharedPreferences encrypted with this question (just in case this affects my problem and I have no idea).
Now if I want to edit a sharedPreference in process=":UnityKillsMe", the edits are not beeing accessable in mainProcess, it just takes the old version of the SharedPreferences. Not until I kill the mainProcess and launch the app again. If I do not kill the mainProcess, and edit the SharedPreferences, while they have been edited in process=":UnityKillsMe", the edits will be overwritten, and are lost.
How do I refresh SharedPreferences in the main process ?
The current implementation of SharedPreferences in Android is not process-safe.
From the docs:
Note: currently this class does not support use across multiple
processes. This will be added later.
The question is do you really need multiple processes in your application?
If you really do, i would suggest you to take a look at Tray, or consider some other form of persistent storage, such as a database.
Check out this answer also.
I am working on an App in android studio. The only thing is that if you close the app you need to start over with the game. Do you guys know how i can integrate a autosave option in my app that every time the user closes the app and start it up again that they don't need to start over?
onSavedInstanceState is used when recreating an activity due to things like orientation changes. check this link: http://developer.android.com/training/basics/activity-lifecycle/recreating.html
If the user is exiting the application completely then you should look into persisting the data with a sqlite db or using shared preferences during the activity lifecycle, check this link: http://www.herongyang.com/Android/Android-Application-Activity-Lifecycle.jpg
When the user starts the app again then check the persisted data during onCreate()
I know it's not the best practice, but it is a requested feature (e.g. for business apps). When the App gets installed, I need to automatically place it on the home screen. I saw a lot of code on the internet, but all of the code only works on button click when the app starts. But I need to place a shortcut on the homescreen immediately after the app is installed and before the app starts. Is there a way I can achieve this?
PS: To make the question more clear: The app will be distributed without Google Play Store, so that's not an option.
Thanks for your help!
Best regards,
Robin
You can't run any code in your application before the user starts your application for first time during to restrictions since Android 3.1. Check this for more information
That's an optional feature of the Market app, not of the apps themselves. When designing an application , it does not receive a broadcast about itself being installed.
the first time the app launched can create it.
You can achieve that by creating a method in your main activity and call this method from oncreate and create a boolean variable andsave it in shared preferences
I am having trouble clearing my app's activity stack. At the start of my app I make the user login and give them a session id. After they login they are able to continue on using the app. However if there session expires I want to redirect them to the login activity and clear the Activity history so they can't have access to the app. I looked at the Android API and the Intent flag FLAG_ACTIVITY_CLEAR_TASK seems to be want I want but it was just included in API level 11 and no phones have the new OS yet. Does anyone have a solution to this problem. Thanks.
I found my answer here. Turns out that I have to broadcast an intent to tell all of the Activities to call the method finish().
The documentation for FLAG_ACTIVITY_CLEAR_TOP describes the situation you want if you use it in conjunction with FLAG_ACTIVITY_NEW_TASK
http://developer.android.com/reference/android/content/Intent.html#FLAG_ACTIVITY_CLEAR_TOP
"This launch mode can also be used to good effect in conjunction with FLAG_ACTIVITY_NEW_TASK: if used to start the root activity of a task, it will bring any currently running instance of that task to the foreground, and then clear it to its root state. This is especially useful, for example, when launching an activity from the notification manager."