1

I am making an android application which has a main activity and several other ones. The main activity(As well as some of the other ones) load data fra SharedPreferences. But, when i launch a new activity, which has some options that can change the data in SharedPreferences that i use in the main activity, and i return to the main activity using the back button, the data there is still the same(Same as before i changed it), somehow i have to reload the data there from SharedPreferences once i return to the main activity, how is this possible??? Please help me and thanks so much in advance!

Main activity code:

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;
    }
}
}
user1446632
  • 417
  • 2
  • 9
  • 24

2 Answers2

1

I fixed it:

@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");
}
user1446632
  • 417
  • 2
  • 9
  • 24
  • Ummm... this is not a very good example. The passed in `prefs` argument is the shared preference that changed. No need to get it again. This also assumes that there is only one shared preference. You need to use the key to identify which one changed if there are more than 1. – jsmith Aug 15 '12 at 18:08
0

So long as your activity remains alive, you can use a listener. See this answer for details:

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

Community
  • 1
  • 1
jsmith
  • 4,847
  • 2
  • 32
  • 39