应用重启时清除共享首选项

时间:2015-04-23 10:48:16

标签: java android sharedpreferences

我在Android应用程序中使用共享首选项 它运行正常,但是当我重新启动应用程序时,所有共享首选项值都消失了。

为什么?

5 个答案:

答案 0 :(得分:0)

在偏好设置中设置值:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<a class="link-questions" href="#faq1">What does this include?</a>

<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br> Asperiores in aperiam error corrupti labore perspiciatis suscipit debitis iusto.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo rerum quidem ipsam possimus, eius nisi laudantium! Unde laudantium quod accusantium est, doloribus! Repellat, minima commodi quibusdam fugiat, provident ipsum nobis.</p>

<div class="newfaqs">
<h5>If I don’t receive rent for a period, can I claim for this?</h5>
<p>You can claim for loss of rent if your building is uninhabitable due to damage or an accident which is covered by the rest of the policy (see: What does landlord insurance cover me for?). Some of the policies offered by our panel can also insure your property whilst it is empty however, this only covers you for damage whilst the property is already empty and therefore won’t cover your lost rent.<br><br>
Finally, some insurers on our panel offer an extension to their policy which means you can claim for loss of rent if the tenant can’t/hasn’t paid you. Please be aware though, that without this extension, you can only claim for loss of rent if your property is uninhabitable due to damage.</p>
<h5>Does landlord insurance cover me for loss of keys?</h5>
<p>Yes , we can cover loss of keys, but also for the replacement of locks and damage from an emergency access too.
Say your tenants vacate your property and keep a set of keys, or whilst unoccupied you lose the master set? Replacing the locks and keys or worse having to break in, in an emergency during the time you lose your keys, can cost a considerable amount. We can offer peace of mind knowing these easy accidents can be covered by your landlord insurance.</p>
<h5>I’ve been told you offer the cheapest landlord insurance, but my online quote is not the cheapest?</h5>
<p>Whilst our online insurance portal offers our best rates to a large proportion of our landlord customers, we always advise speaking to our advisors directly if you have anything out of the ordinary, want specific cover for something or want to discuss ways to get cover even cheaper. We can often discuss lowering areas of cover you may not need, or want, or help you source a discount.
<br><br>
As a specialist broker, we pride ourselves on understanding our customers’ needs and finding them with a policy that offers the best landlord insurance, at the cheapest price. However, sometimes this is best done over the phone.</p>
<h5>I’ve had trouble finding landlord insurance for my coach house, can you help?</h5>
<p>A lot of landlords with coach houses to rent find it a struggle to get the right cover (or any cover at all) with high street insurers or online.  Because we have access to large proportions of the insurance market and have good relationships with the insurers themselves we can find policies that others can’t.  You will need to speak directly to one of our advisors though!</p>
<h5>I can’t find landlord insurance for my separate garage, please help!</h5>
<p>As long as the building is on the deeds to the property, be it a garage, out building or annex we’ll be able to help you find the right policy. The tenant will need to occupy the property too. This means if you rent the garage or annex separately you may need to look to cover these on separate policies.</p>
<h5>If my tenant(s) trashes my property can I claim?</h5>
<p>We can provide cover for malicious damage. This doesn’t necessarily come as standard on all policies from our panel of insurers so make sure you ask us for it when you call us or get a quote online. 
<br><br>
With malicious damage cover in place, if a tenant damages your building or your contents, you’ll be able to claim and we’ll do our best to recover the cost of the damage caused. So if a tenant spray paints your walls or slashes your sofas, we’ll sort it for you. </p>
<h5>Will my policy cover me for subsidence?</h5>
<p>If you need cover for subsidence, we can provide it. However, there is a “but” – if you your property has a history of subsidence we’ll require an inspection of the property to establish the risk of further damage before we provide cover. </p>
<h5>How does having a different type of tenant such as DSS or student affect my policy?</h5>
<p>Insurance is all about covering risks. Statistically, tenants such as DSS and students pose more of a risk to your property. After all, it’s no secret that students like to party! 
<br><br>
Some insurers or brokers simply won’t cover you if you have high risk tenants living in your property. However, we can provide cover for a wide range of tenant types. So, if that student party gets a bit out of hand and a window gets broken for example, you’ll be covered. </p>
<h5>I can’t find landlord insurance because of a history of subsidence.</h5>
<p>The simple fact of the matter is all insurers are wary of properties with subsidence, even if it was 10 years ago! Usually we won’t have trouble finding you insurance as long as you can provide the correct paper work to prove the initial cause has been dealt with and subsequent checks have been made. Speak to us on the phone, or via email, and we can discuss your options with you.</p>
<h5>How do I calculate my sums insured?</h5>
<p>Many of our customers often assume the rebuild price is the same as the market value. This however is not the most accurate way to understand the costs because this value does not include the demolition and debris removal of the previous property, or the architects, materials or labour costs to re-build.  Not forgetting, many insurers will charge a penalty, for underinsuring a property so it’s very important this is correct.
<br><br>
We advise all our customers to use a rebuild calculator or professional service to get the best landlord insurance cover for their property.</p>
<h5 id="faq1">What does landlord insurance cover me for?</h5>
<p>The standard perils landlord insurance covers you for are the same as standard home insurance. They are things like fire, floods, escape of water, subsidence etc.  The differences can be things like loss of keys, loss of rent, conditions on leaving it unoccupied etc.  Our online quote system or advisor will be able to discuss your requirements and find something that suits your needs exactly. </p>
<h5>I need landlord insurance for multiple properties.</h5>
<p>That’s great. We can help you here too!<br><br>
We specialise in finding great insurance policies for landlords with multiple properties.  In fact if you have between 1 and 50 properties we can often find you discounts on the policies and the administration fees
<br><br>
We can also include any new policies pro-rata, meaning you can keep the same renewal date for all you properties.
</p>
<h5>I’ve been a landlord for a number of years and I’ve never had to claim. Will this reduce my premium?</h5>
<p>Absolutely. We really value experienced landlords and know that when you’ve been at it for a while, you tend to be lower risk. With that in mind, we reward careful and experienced landlords with no claims discounts. </p>
                </div>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br> Asperiores in aperiam error corrupti labore perspiciatis suscipit debitis iusto.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo rerum quidem ipsam possimus, eius nisi laudantium! Unde laudantium quod accusantium est, doloribus! Repellat, minima commodi quibusdam fugiat, provident ipsum nobis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br> Asperiores in aperiam error corrupti labore perspiciatis suscipit debitis iusto.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo rerum quidem ipsam possimus, eius nisi laudantium! Unde laudantium quod accusantium est, doloribus! Repellat, minima commodi quibusdam fugiat, provident ipsum nobis.</p>
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Qui voluptate assumenda, dolore doloribus sint vitae, eligendi odio cupiditate nobis ex perferendis cum, optio commodi omnis veniam alias! Labore, iste, corporis!Lorem ipsum dolor sit amet, consectetur adipisicing elit. Voluptates atque laudantium voluptas, a autem! Ratione nulla dolore corporis laborum labore!<br><br> Asperiores in aperiam error corrupti labore perspiciatis suscipit debitis iusto.Lorem ipsum dolor sit amet, consectetur adipisicing elit. Illo rerum quidem ipsam possimus, eius nisi laudantium! Unde laudantium quod accusantium est, doloribus! Repellat, minima commodi quibusdam fugiat, provident ipsum nobis.</p>

从偏好中检索数据:

// MY_PREFS_NAME - a static String variable like: 
// public static final String MY_PREFS_NAME = "MyPrefsFile";
   SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME,    MODE_PRIVATE).edit();
   editor.putString("name", "Elena");
   editor.putInt("idName", 12);
   editor.commit();

更多信息
http://developer.android.com/guide/topics/data/data-storage.html#pref http://developer.android.com/reference/android/content/SharedPreferences.html

答案 1 :(得分:0)

没有代码就难以解决。无论如何,我想你不会在每次启动它之前重新安装应用程序。因此,您可能不会对共享首选项进行更改。来自Saving Key-Value Sets

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.saved_high_score), newHighScore);
editor.commit(); 

检查您的代码以验证是否存在commit()指令。

答案 2 :(得分:0)

第一,你是否在if语句中做了更多的事情?因为您创建了两个不执行任何操作的变量,并在此子句之后被销毁 你甚至分配了&#34; text&#34;因为你得到了默认的空值

,所以当它没有加载时就没有了

对不起,我以为这是你的代码,但后来又去了assad

答案 3 :(得分:0)

我认为你没有做出这些改变。在放置值之后必须应用SharedPreferences.Editor.commit()。提交确保已保存值。

答案 4 :(得分:0)

考虑这个接受的答案:

我不知道为什么,但只需将你的prefs代码放在异步任务中就可以了:

prefss = getSharedPreferences(ACCOUNT_PREFS_NAME, MODE_MULTI_PROCESS);
new AsyncSave(favNamesList).execute();
private static class AsyncSave extends AsyncTask<Void, Void, Boolean> {

String favNamesList;

AsyncSave(String favNamesList) {
    this.favNamesList = favNamesList;
}

@Override
protected Boolean doInBackground(Void... params) {
    prefss.edit().putString("favNamesList", strings).apply();
    return null;
    }
}