Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to ensure user selected image doesn't change after restart Android?

I have a simple sticker widget that allows the user to select one of the stickers upon placing the widget. My problem is that once the device is restarted, the sticker reverts to the default one. How can I ensure that the user selected sticker remains regardless of whether the device is restarted or not? An example or push in the right direction would be very helpful. I'm not looking for anyone to do it for me, just to be educated on how to go about doing that. Thank you.

EDIT Included Code

I'm not sure but I think I'm going about this all wrong. I have no clue how to incorporate SharedPreferences the way I have it now. Could someone give me an example of how I should go about doing this with at least one of the images I'm using please?

public class SpraycanConfig extends Activity
{
int xawID;
AppWidgetManager xawm;
Context xc;
EditText xinfo;
//EasyTracker easyTracker;

protected void onCreate(Bundle paramBundle)
{
 super.onCreate(paramBundle);

 Crashlytics.start(this);

setContentView(R.layout.scconfiglayout);



  this.xc = this;
  Bundle localBundle = getIntent().getExtras();
  if (localBundle != null) {
  this.xawID = localBundle.getInt("appWidgetId", 0);
  }
  for (;;)
  {
    this.xawm = AppWidgetManager.getInstance(this.xc);
    ImageButton localImageButton1 = (ImageButton)findViewById(R.id.normal);
View.OnClickListener local1 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.normal);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();
  }
};
localImageButton1.setOnClickListener(local1);
ImageButton localImageButton2 = (ImageButton)findViewById(R.id.belton);
View.OnClickListener local2 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.belton);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton2.setOnClickListener(local2);
ImageButton localImageButton3 = (ImageButton)findViewById(R.id.gold);
View.OnClickListener local3 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.gold);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton3.setOnClickListener(local3);
ImageButton localImageButton4 = (ImageButton)findViewById(R.id.hardcore);
View.OnClickListener local4 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.hardcore);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton4.setOnClickListener(local4);
ImageButton localImageButton5 = (ImageButton)findViewById(R.id.ironlak);
View.OnClickListener local5 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.ironlak);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton5.setOnClickListener(local5);
ImageButton localImageButton6 = (ImageButton)findViewById(R.id.krylon);
View.OnClickListener local6 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.krylon);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton6.setOnClickListener(local6);
ImageButton localImageButton7 = (ImageButton)findViewById(R.id.mtn94);
View.OnClickListener local7 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.ninetyfour);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton7.setOnClickListener(local7);
ImageButton localImageButton8 = (ImageButton)findViewById(R.id.rusto);
View.OnClickListener local8 = new View.OnClickListener()
{
  public void onClick(View paramAnonymousView)
  {
    RemoteViews localRemoteViews = new RemoteViews(xc.getPackageName(), R.layout.main);
    localRemoteViews.setImageViewResource(R.id.imageview, R.drawable.rusto);
    xawm.updateAppWidget(xawID, localRemoteViews);
    Intent localIntent = new Intent();
    localIntent.putExtra("appWidgetId", xawID);
    setResult(-1, localIntent);
    finish();

  }
};
localImageButton8.setOnClickListener(local8);

return;

  }

 }

@Override
protected void onStart() {
    super.onStart();

}

@Override
protected void onStop() {
    super.onStop();

 }



}
like image 741
Steve C. Avatar asked Nov 02 '15 04:11

Steve C.


Video Answer


2 Answers

SharedPreferences would be your best choice.

Actually you can save that the selected image in any way of storage used in android.

like image 138
N Jay Avatar answered Oct 24 '22 15:10

N Jay


Exactly like that:

protected void onCreate(Bundle paramBundle)
{
    super.onCreate(paramBundle);
    SharedPreferences sharedPreferences = getSharedPreferences("myPref", 0);

    // get the last chosen sticker
    int sticker = sharedPreferences.getInt("chosenSticker", R.drawable.normal);  //the .normal is the default value in case the user didn't choose one.
    myView.setImageViewResource(R.id.imageview, sticker);  //now you can use it


    // store user's choice for example:
    View.OnClickListener local2 = new View.OnClickListener()
    {
        public void onClick(View paramAnonymousView)
        {
            SharedPreferences.Editor editor = sharedPreferences.edit();
            editor.putInt("chosenSticker", R.drawable.belton);
            editor.apply();

            // your rest of your code here ...
        }
    }
}
like image 29
Eliran Kuta Avatar answered Oct 24 '22 15:10

Eliran Kuta