私はメインアクティビティと他のいくつかのアクティビティを持つ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;
}
}
}