简体   繁体   中英

Scope problem - put intent extra and start activity from 2 different functions

I have 2 listeners, 1 needs to get the intent extra, which is an image uri, and the other one needs to start the other activity with that extra. But I think that due to them being in different scopes, the extra is not really put for the intent that starts the other activity.

This is the code:

        private Intent intent = new Intent(this, OtherActivity.class);
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            final Button button= findViewById(R.id.button);
            button.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    intent = new Intent(v.getContext(), OtherActivity.class);
                    startActivity(intent);
                }
            });


        }
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {

            if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
                Thing.ActivityResult result = Thing.getActivityResult(data);
                if (resultCode == RESULT_OK) {
                    Uri resultUri = result.getUri();
                    intent.putExtra("imageUri", resultUri.toString());    
                }
           }
   } 

I want that the extra that I put in onActivityResult will be sent to the activity started by the onClickListener when the button is clicked

my suggestion is to use a field to store the image URI :

1- define a String variable in your class

String imageURI = "";

2- in your onActivityResult :

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
            Thing.ActivityResult result = Thing.getActivityResult(data);
            if (resultCode == RESULT_OK) {
                Uri resultUri = result.getUri();
                imageURI = resultUri.toString();    
            }
       }

3-in your onClickListener :

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
            intent = new Intent(v.getContext(), OtherActivity.class);
                intent.putExtra("imageUri", imageURI);    
                startActivity(intent);
            }
        });

In the onClick you are re-initializing the Intent, so any extras you may have set are lost. If you remove the line above startActivity you should be fine.

I would not go that way thought. I would not have the Intent as a class variable but rather the image URI. So whenever a click event happens, I would check for the image URI having been set, create a new Intent and then start the Activity.

Your code would become like this:

private String intentImage = null;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final Button button = findViewById(R.id.button);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            if (intentImage != null) {
                Intent intent = new Intent(this, OtherActivity.class);
                intent.putExtra("imageUri", intentImage); 

                startActivity(intent);
            }
        });
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == Thing.SPECIFIC_REQUEST_CODE) {
        Thing.ActivityResult result = Thing.getActivityResult(data);

        if (resultCode == RESULT_OK) {
            Uri resultUri = result.getUri();
            intentImage = resultUri.toString();    
        }
    }
}

Also keep in mind that you don't have any startActivityForResult in your code. So if this is your full activity, you would never get onActivityResult invoked.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM