first one is:
I have a setting activity and when I set my settings and I got back to Main-Activity My settings have not been applied.
Second one is:
I have a SharedPreferences in my setting activity for saving my setting.
but I don't know how can I use that when I start my app for the first time because my Shared-Preferences isn't build up yet. My SharedPreferences is in my Setting activity not in Main Activity. How can I do that?
I'm sorry if it's stupid question:)
please help.
Related
I have an activity to set my preferences the first time that application get opened, but how can I change activity status (move the "MAIN" on another activity)
You cannot move the MAIN Activity to another activity at runtime. It is mandatory to define it while writing Manifest file. If you want to do that as per preferences values then check the preference value everytime and then move to your required activity.
Open SplashScreenActivity--> check preference value here-->If false go to your FirstActivity when app launched newly-->If true go to your AnotherActivity.
Hope you got the concept.
I have 2 Fragments. 1st is for calculation while the 2nd is for settings.
I save the settings using SharedPreference.Editor.commit() on onPause() method. No problem so far with the saving. The problem I am facing is retrieving the SharedPreference value on my 1st Fragment. I retrieved the value every time I pressed the count button. For the 1st time, the values I am getting are the ones before change (which is the problem I am facing), I will only get my saved value when retrieving/pressing the count button for the 2nd time or more.
And I try to change the settings and then press home button to terminate the app from outside (which triggers onPause method) and when I reopened the settings, the values did change to my defined settings. So, I am sure the settings did saved when onPause is triggered.
I wonder what is going wrong here. Any helps is much appreciated.
As requested, this is my saving code on my SettingFragment :
#Override
public void onPause() {
super.onPause();
saveToPref();
}
public void saveToPref() {
SharedPreferences settings = getActivity().getSharedPreferences("mysettings", 0);
Editor edit = settings.edit();
edit.putString("begin", String.valueOf(ibegin)).putString("end", String.valueOf(iend)).commit();
}
Initialize your SharedPreferences object somewhere else, and do it once - maybe in onCreate. The reason why you're not seeing the change is because a different instance of SharedPreferences is opened somewhere, with the same constructor, and multiple instances of these do not automatically resolve/merge. While you're at it, initialize the Editor edit instance along with settings.
Ok, I have found my problem. The problem is that onPause() is triggered only when I start another activity (I started an activity onClick of Button count). I changed my code to trigger the saveToPref() by overriding onPageSelected of my ViewPager and on backPressed of my MainActivity.
I am developing an application.
my requirement is that first time when i installing the application in to device it has to start the main and launcher activity.
After that when i am starting/opening my application in side the device it has to load another activity instead of main and launcher.
If the application is uninstalls and installs again it has to load the main and launcher again.
can you please anybody share the solution on this kind of topics.
Thanks in advance.
You can do this:
Say Activity A is the activity that you want to launch only the first time, and activity B the activity that the system will launch after the first time.
In you manifest put Activity B as your launcher activity. Then inside the oncreate or better OnResume of activity B put the following:
#Override
protected void onResume() {
super.onResume();
if(firstLaunch()){
startActivity(new Intent(this, A.class));
finish();
}else{
//Do your normal stuff
}
}
private boolean firstLaunch(){
SharedPreferences prefs = getSharedPreferences(
"Preferences",
Context.MODE_PRIVATE);
return prefs.getBoolean("firstLaunch",false);
}
Then on your A activity be sure to set a flag on your preferences to indicate that your application has run more than once. So somewhere inside activity A put this:
private void setFirsLaunchFlag(){
SharedPreferences prefs = getSharedPreferences(
"Preferences",
Context.MODE_PRIVATE);
SharedPreferences.Editor edit = prefs.edit();
edit.putBoolean("firstLaunch",true);
edit.commit();
}
You can use a bool value like this:
1. When application is being launched for the first time make it true.
2. Check this bool value in your launcher activity and if it is true start your desired activity and if it false then make it true and save (for the first time).
Note: You can use SharedPreference for the bool value.
I think this may help you:
If the App is first time launched android.intent.action.PACKAGE_FIRST_LAUNCH broadcast will be fired.
After receiving this broadcast maintain a flag in shared preference that is the app is launched first time.
Have a splash screen activity as your launcher activity and according to that flag redirect it to your one time activity or replacing activity.
As far as ı know you don't have a chance to launch another activity.
Instead you could keep the track of your installation inside SharedPreferences and run
different code based on that.
simply create an integer or Boolean shared preference variable.when ever the application starts at first instance display the activity which you want to be displayed just 1'c, at the same time change the value of this shared preference variable.every time from not when the app is just started you have to check this value and and if it is not the default value load another activity in main and launcher.
Here's a reference LINK
, not exactly the expected ans..but hope it helps
hi all
i have an app with 3 activity. if i launch the app the first time it have to redirect to second activity from first activity. after the first time launch the app it have to go to third activity from first activity. how to do that? please help me. is there way to without using database?
Set a boolean flag in the SharedPreferences. On loading your application you can read that flag.
If it is null: goto Activity 2 and set the flag to true
If it exists: goto Activity 3
I am developing an application where one of my Activity contains a button as "Set as Home Page".
So my problem is that when I will click this button the status will be saved in the shared preference and next time when this application will be opened I want to start this Activity (the Activity which has been set as home page) instead of the default one.
So how can i do it???
You need to have defined static constant unique ID for each of your Activity. You save this ID to shared preferences, and implement on your boot activity's onCreate event a switch based on this stored ID against the static constant ID of your Activity. When you have the right step start the activity, and finish your current activity the booter.
You could create a kind of redirection Activity on which your application would start on. Then, put a switch in this activity, with intents sending to each of your activities, and the state of the preference would be the variable to test for the switch.
I'm not sure I'm clear but tell me if it's fine for you?