List set in onActivityResult with data not sticking

193 views Asked by At

I need to set a List variable using data returned from onActivityResult. Then use that list to do things when a button is pressed.

If I put a Log.d of the list right after setting it inside onActivityResult, it correctly shows the list. But if I put a second Log.d of the list inside onResume or when the button is pressed, the second log shows the list as null.

Not full code, just relevant parts.

public class ImageSelectActivity extends AppCompatActivity {
    public static List<String> path;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_select);
    Button btnSelectImages = (Button) findViewById(R.id.btnSelect);
    btnSelectImages.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MultiImageSelector selector = MultiImageSelector.create(ImageSelectActivity.this);
            selector.count(12);
            selector.showCamera(true);
            selector.start(ImageSelectActivity.this, REQUEST_IMAGE);
        }
    });

    Button btnImagesLog = (Button) findViewById(R.id.btnImages);
    btnImagesLog.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Log.d("path",""+path); //path returns null even AFTER clicking the select button and selecting images.
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_IMAGE){
        if(resultCode == RESULT_OK){
            // list of image paths
            List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            Log.d("images",""+path); //correctly displays path of all selected images.
        }
    }
}
2

There are 2 answers

1
GhostCat On BEST ANSWER

Your problem is shadowing:

You declare a completely new list object in that method:

// list of image paths
List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);

That is a local variable! Thus you are not assigning a value to ImageSelectActivity.path, but to that local variable. And ImageSelectActivity.path just keeps its old value.

So, the answer is simply: turn that lines into

// list of image paths
path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);

and you should be good. Well, except for the thing that using static variables is often not a good idea.

Beyond that: this is really basic stuff. If you don't know about such things, I recommend you to first study education material on those Java basics before further engaging with Android. For example: work your way through the Oracle tutorials; at least the basic ones!

0
Pavneet_Singh On

You are creating a new reference variable and assigning list to it

List<String> path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT); 
// ^^^^^ new local variable 

so you need to use the already defined variable so just do it like this

 public static List<String> path;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == REQUEST_IMAGE){
        if(resultCode == RESULT_OK){
            // list of image paths
            path = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
           //^^^^ don't create a local variable , 
           //just refer the one already defined in class 
            Log.d("images",""+path);
        }
    }
}