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.
}
}
}
Your problem is shadowing:
You declare a completely new list object in that method:
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
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!