重新加载SharedPreferences数据

时间:2012-06-12 15:32:48

标签: android android-activity sharedpreferences reload

我正在创建一个具有主要活动和其他几个的Android应用程序。主要活动(以及其他一些活动)从SharedPreferences加载数据。但是,当我启动一个新活动时,它有一些选项可以改变我在主活动中使用的SharedPreferences中的数据,并且我使用后退按钮返回主活动,那里的数据仍然是相同的(相同的在我改变之前),不知怎的,我必须从SharedPreferences重新加载数据,一旦我返回主要活动,这怎么可能?请帮助我,并提前致谢!

主要活动代码:

package com.mycompanyname.myappname;

import java.util.Calendar;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class myActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
TextView nextAirFilterCleaningTextView, nextPistonChangingTextView, nextOilChangingTextView;
Button manageButton;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    //Import views
    manageButton = (Button)findViewById(R.id.manageButton);
    nextAirFilterCleaningTextView = (TextView)findViewById(R.id.nextAirFilterCleaningTextView);
    nextPistonChangingTextView = (TextView)findViewById(R.id.nextPistonChangingTextView);
    nextOilChangingTextView = (TextView)findViewById(R.id.nextOilChangingTextView);

    //Setup onClickListener for the buttons
    manageButton.setOnClickListener(this);

    //Check if the user has been using his motorcycle
    android.content.Context ctx = getApplicationContext();
    Intent i = new Intent(ctx, UsageActivity.class);
    i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    ctx.startActivity(i);

    //Load first time use screen
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    boolean previouslyStarted = prefs.getBoolean(getString(R.string.pref_previously_started), false);
    if(!previouslyStarted){
    SharedPreferences.Editor edit = prefs.edit();
    edit.putBoolean(getString(R.string.pref_previously_started), Boolean.TRUE);
                edit.commit();
                android.content.Context ctx5 = getApplicationContext();
                Intent i5 = new Intent(ctx5, FirsttimeusageActivity.class);
                i5.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                ctx5.startActivity(i5);
}

    //Load information from SharedPreferences
    SharedPreferences settings = getSharedPreferences("settingsInfo", 0);
    nextAirFilterCleaningTextView.setText("Next air filter cleaning: " + settings.getString("daysTillAirFilterCleaning", "") + " days");
    nextPistonChangingTextView.setText("Next piston changing: " + settings.getString("hoursTillPistonChange", "").toString() + " hours of usage left");
    nextOilChangingTextView.setText("Next oil changing: " + settings.getString("hoursTillOilChange", "").toString() + " hours of usage left");
}
@Override
public boolean onCreateOptionsMenu(Menu meny) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menubuttons, meny);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
    case R.id.NewEvent:
        //Create new calendar event
        Calendar cal = Calendar.getInstance();              
        Intent intent = new Intent(Intent.ACTION_EDIT);
        intent.setType("vnd.android.cursor.item/event");
        intent.putExtra("beginTime", cal.getTimeInMillis());
        intent.putExtra("allDay", true);
        intent.putExtra("rrule", "FREQ=YEARLY");
        intent.putExtra("endTime", cal.getTimeInMillis()+60*60*1000);
        intent.putExtra("title", "");
        startActivity(intent);
        break;
    case R.id.About:
        //Load about activity and screen
        android.content.Context ctx6 = getApplicationContext();
        Intent i6 = new Intent(ctx6, AboutActivity.class);
        i6.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx6.startActivity(i6);
        break;
    case R.id.settings:
        //Load settings activity and screen
        android.content.Context ctx4 = getApplicationContext();
        Intent i4 = new Intent(ctx4, settingsActivity.class);
        i4.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx4.startActivity(i4);
        break;
    case R.id.Homepage:
        //Load webpage by using custom activity
        android.content.Context ctx = getApplicationContext();
        Intent i = new Intent(ctx, Activity.class);
        i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx.startActivity(i);
        break;
    case R.id.famoustracks:
        android.content.Context ctx7 = getApplicationContext();
        Intent i7 = new Intent(ctx7, Activity.class);
        i7.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx7.startActivity(i7);
        break;
        }
    return true;
}
public void onClick(View src) {
    switch(src.getId()) {
    case R.id.manageButton:
        android.content.Context ctx1 = getApplicationContext();
        Intent i1 = new Intent(ctx1, ManageActivity.class);
        i1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        ctx1.startActivity(i1);
        break;
    }
}
}

2 个答案:

答案 0 :(得分:1)

我修好了它:

@Override
public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    //Load information from SharedPreferences
    SharedPreferences settings = getSharedPreferences("settingsInfo", 0);
    nextAirFilterCleaningTextView.setText("Next air filter cleaning: " + settings.getString("daysTillAirFilterCleaning", "") + " days");
    nextPistonChangingTextView.setText("Next piston changing: " + settings.getString("hoursTillPistonChange", "").toString() + " hours of usage left");
    nextOilChangingTextView.setText("Next oil changing: " + settings.getString("hoursTillOilChange", "").toString() + " hours of usage left");
}

答案 1 :(得分:0)

只要您的活动仍然存在,您就可以使用监听器。有关详细信息,请参阅此答案:

How to update main activity to know that prefs are changed?