1

I've asked this before, but my understanding has increased slightly. I've figured out how to get the user to choose a custom background image on a layout. I use this:

in my onCreate method:

Button player = (Button) setBg.findViewById(R.id.plBg);

                player.setOnClickListener(new OnClickListener() {
                    public void onClick(View v) {

                        Intent i = new Intent(Intent.ACTION_PICK, 
                                android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                        i.setDataAndType(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, "image/*");                                      
                        startActivityForResult(i, RESULT_LOAD_PLAYER);

                        setBg.dismiss();
                    }
                });

and in my onActivityResult method:

    @Override
protected void onActivityResult(int requestCode, int resultCode, final Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RESULT_LOAD_PLAYER && resultCode == RESULT_OK && null != data) {
         Uri selectedImage = data.getData();
         String[] filePathColumn = {MediaStore.Images.Media.DATA};

         Cursor cursor = getContentResolver().query(selectedImage,
                 filePathColumn, null, null, null);
         cursor.moveToFirst();

         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
         String picturePath = cursor.getString(columnIndex);
         cursor.close();

         playerBg = (ImageView) findViewById(R.id.playerBg);
         playerBg.setImageBitmap(BitmapFactory.decodeFile(picturePath));
         playerBg.setScaleType(ScaleType.CENTER_CROP);
     }
}

but when you 'back' out of the app, the background reverts back to default. how do I get that background choice to stick?

I've already look at this: Save bitmap to location

But I have a hard time understand it.
I've also tried getting it to save in shared preferences but learned that that's not what they're for. I'm still a beginner that this. Thanks in advance.

Community
  • 1
  • 1
Psest328
  • 6,575
  • 11
  • 55
  • 90
  • above code is from what method? – Marcin Orlowski Sep 11 '12 at 21:19
  • sorry, that's in my onActivityResult. I'll edit the OP to make it clearer – Psest328 Sep 11 '12 at 21:21
  • 1
    Why not have a flag in sharedpreference which indicates that user set any background or not? And also, if user set any background then you can save the picture path in sharedpreference. So, every time when your activity starts. You check the flag from sharedpref and set the background using the picture path saved in shared pref. – VendettaDroid Sep 11 '12 at 21:22
  • you should not call `super.onActivityResult()` if you handled this result yourself – Marcin Orlowski Sep 11 '12 at 21:28
  • I have a few things going on in onActivityResult, that's why I'm calling super – Psest328 Sep 11 '12 at 21:31

2 Answers2

1

IMO, you can have flag(boolean var) in sharedpreference which indicates that user set any background or not? And also, if user set any background then you can save the picture path in sharedpreference. So, every time when your activity starts which onCreate(). You check the flag from sharedpref and set the background using the picture path saved in shared pref.

Also, I agree with matheszabi, you need to know more about android lifecycle.

VendettaDroid
  • 3,131
  • 2
  • 28
  • 41
0

If you are a beginner, than you wouldn't appreciate the "know how", but maybe based on this somebody will write for you a 100% working code, which is ready for copy-paste.

but when you 'back' out of the app, the background reverts back to default.

That is because you don't know the Activity Lifecycle methods. You should load the saved bacground again in OnCreate. Please take a look here for Activity lifecycle methods.

Hope it helps somebody.

  • I understand the lifecycle. I know WHY it's not persisting... but I can't figure out how to make it persist. I've done it so far with basic types such as strings and ints, but this is killing me – Psest328 Sep 11 '12 at 21:25
  • "You should load the saved bacground again in OnCreate" –  Sep 11 '12 at 21:28
  • http://stackoverflow.com/questions/3740784/store-image-and-audio-file-in-android-internal-storage –  Sep 11 '12 at 21:30