我正在制作一个具有主要活动和其他几个活动的 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;
}
}
}