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.
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.