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

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.

Related

What is the correct way to handle multiple requestCodes in onActivityResult()?

I have two activities and the second activity should send back a string of text to the appropriate textview on activity 1 depending on if I select button 1 or button 2. I believe my intent in launchFoodActivity() in activity 1 is incorrectly asking for 2 requests at the same time. How can I correct my code to have each string of text be sent to the appropriate textview in activity 1?
Activity 1
public class MainActivity extends AppCompatActivity {
private TextView mReplyTextView1;
private TextView mReplyTextView2;
public static final int TEXT_REQUEST = 1;
public static final int TEXT_REQUEST2 = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mReplyTextView1 = findViewById(R.id.food_text_1);
mReplyTextView2 = findViewById(R.id.food_text_2);
}
public void launchFoodActivity(View view) {
Intent intent = new Intent(this, FoodListActivity.class);
startActivityForResult(intent, TEXT_REQUEST);
Intent intent2 = new Intent(this, FoodListActivity.class);
startActivityForResult(intent2, TEXT_REQUEST2);
}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
String textFoodReply1 = data.getStringExtra(FoodListActivity.EXTRA_REPLY);
mReplyTextView1.setText(textFoodReply1);
break;
}
}
case 2:
if (requestCode == 2) {
if (resultCode == RESULT_OK) {
String textFoodReply2 = data.getStringExtra(FoodListActivity.EXTRA_REPLY);
mReplyTextView2.setText(textFoodReply2);
break;
}
}
}
}
}
Activity 2
public class FoodListActivity extends AppCompatActivity {
public static final String EXTRA_REPLY = "com.dev20.shoppinglist.extra.REPLY";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_list);
}
public void returnFoodItem1(View view) {
String replyItem1 = getResources().getString(R.string.cheese_text);
Intent replyIntent = new Intent();
replyIntent.putExtra(EXTRA_REPLY, replyItem1);
setResult(RESULT_OK, replyIntent);
finish();
}
public void returnFoodItem2(View view) {
String replyItem2 = getResources().getString(R.string.apples_text);
Intent replyIntent2 = new Intent();
replyIntent2.putExtra(EXTRA_REPLY, replyItem2);
setResult(RESULT_OK, replyIntent2);
finish();
}
}

Save QR code text in memory and show it in another activity

I have an app that scans QR code. And it to show in another activity. How to pass QR Code text to another activity? Or save it in memory?
MainActivity
public class MainActivity extends AppCompatActivity implements ZXingScannerView.ResultHandler{
private ZXingScannerView zXingScannerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
zXingScannerView = new ZXingScannerView(getApplicationContext());
setContentView(zXingScannerView);
zXingScannerView.setResultHandler(this);
zXingScannerView.startCamera();
}
#Override
protected void onPause() {
super.onPause();
zXingScannerView.stopCamera();
}
#Override
public void handleResult(Result result) {
Toast.makeText(getApplicationContext(), result.getText(),Toast.LENGTH_SHORT).show();
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Result");
builder.setMessage(result.getText());
AlertDialog alertDialog = builder.create();
alertDialog.show();
zXingScannerView.resumeCameraPreview(this);
}
}
Call method startActivityForResult(new Intent(this, YourCallbackActivity.class), 1001);
Use setResult() method for get call back in another activity.
#Override
public void handleResult (Result result) {
Intent returnIntent = new Intent();
returnIntent.putExtra("result",String.valueOf(rawResult));
setResult(Activity.RESULT_OK,returnIntent);
finish();
}
now
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1001) {
if (resultCode == RESULT_OK && data != null) {
String result = data.getStringExtra("result");
//do whatever you want
}
}
}
I already did, hope it will help you!!

Share image taking from Camera

I am developing an app which takes a picture with the camera and allows the user to share it. But the share intent isn't working. I have checked but none are working on my device. I am running the app on an Android 6.0 marshmallow tablet.
Here is my code
public class SharePic extends Activity {
static final int REQUEST_IMAGE_CAPTURE = 1;
ImageView picView;
Button shareBtn;
Bitmap imageBitmap;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_share_pic);
picView = (ImageView) findViewById(R.id.picView);
shareBtn = (Button) findViewById(R.id.shareBtn);
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
imageBitmap = (Bitmap) extras.get("data");
picView.setImageBitmap(imageBitmap);
}
}
public void share(View view){
String bitmapPath = MediaStore.Images.Media.insertImage(getContentResolver(), imageBitmap,"title", null);
Uri bitmapUri = Uri.parse(bitmapPath);
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, bitmapUri);
startActivity(Intent.createChooser(intent , "Share"));
}
}
Any help?
So, when is the share button being called.
I guess you might have to set event listener setOnClickListener on share button click in case you haven't set it in the XML
shareBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
share(v);
}
});

Play videoView on another activity

I have an ImageButton which plays the video I recorded. Everything works fine but the thing is I want to play the recorded video(resultvideo) on another activity. I'm new to Android Dev. could someone teach me how to do that, thanks!
Here's my code:
public class MainActivity extends AppCompatActivity {
ImageButton imageButton;
static final int REQUEST_VIDEO_CAPTURE = 1;
VideoView resultvideo;
MediaController mediacontroller;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultvideo = (VideoView)findViewById(R.id.videoView);
mediacontroller = new MediaController(MainActivity.this);
mediacontroller.setAnchorView(resultvideo);
resultvideo.setMediaController(mediacontroller);
Button click = (Button)findViewById(R.id.buttonRecord);
resultvideo = (VideoView)findViewById(R.id.videoView);
}
public void dispatchTakeVideoIntent(View v) {
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
if (takeVideoIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
Uri videoUri = data.getData();
resultvideo.setVideoURI(videoUri);
resultvideo.pause();
}
imageButton = (ImageButton) findViewById(R.id.imageButton);
{
imageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, Main2Activity.class));
resultvideo.start();
}
});
}
}
}
You can send Uri of video via Intent to another activity
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("VIDEO_URI", videoUri.toString());
startActivity(intent);
And then in Main2Activity in onCreate method get videoUri like this:
String uri = getIntent().getStringExtra("VIDEO_URI");
Uri videoUri = Uri.parse(uri);

startActivityForResult returning null when back navigation to activity sending result returns

I have 3 activities that start in a sequence.With the first activity needing a result from the last activity.
I have it so that if Activity B is started for result (By activity A) then Activity B starts activity C for result. Then once the result is captured at activity C it is finished which calls Activity B's onActivityResult which sets the result and finishes and Activity A's onActivityResult gets the final result.
Activity A starts Activity B for a result which Activity C contains thus the sequence is like so
A->B->C (get result) C->B->A (result retrieved)
This works just fine if everything happens in sequence. However, If I navigate to activity C then press the toolbars back arrow and which leads me to activity B then navigate back to activity C and select the result. The result returned to activity A is null.
A->B->C->B->C(get result) C->B->A (result == null)
Activity A
public class AlertCreationActivity extends AppCombatActivity {
// OnCreate methods left out to shorten code
#OnClick(R.id.locationButton)
public void locationButtonClicked() {
Intent intent = new Intent(this, StateActivity.class);
intent.putExtra(StateActivity.IS_STARTED_FOR_RESULT, StateActivity.STARTED_FOR_RESULT);
startActivityForResult(intent, ALERT_CREATION_REQUEST_CODE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == ALERT_CREATION_REQUEST_CODE && resultCode == RESULT_OK) {
String title = data.getStringExtra(ALERT_CREATION_REQUEST_DATA);
if ( ! title.isEmpty()) {
mLocationButton.setVisibility(View.INVISIBLE);
mLocationTextView.setText(title);
mLocationTextView.setVisibility(View.VISIBLE);
}
}
}
}
Activity B
public class StateActivity extends AppCompatActivity {
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AlertCreationActivity.ALERT_CREATION_REQUEST_CODE) {
Intent intent = new Intent();
intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, data.getStringExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA));
setResult(RESULT_OK, intent);
finish();
}
}
}
Activity B's adapter is where the next intent happens
public class StateAdapter extends RecyclerView.Adapter<StateAdapter.StateViewHolder> {
// Other Adapter methods not shown
public class StateViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
// Other View Holder methods not shown
#Override
public void onClick(View v) {
State selectedState = getStateBy(mLocationLabel.getText().toString());
// Save state ID
LocationStorage storage = new LocationStorage(mActivity);
storage.setSelectedStateId(selectedState.getId());
storage.setSelectedStateName(selectedState.getName());
Intent intent = new Intent(mActivity, SpotActivity.class);
mActivity.startActivityForResult(intent, AlertCreationActivity.ALERT_CREATION_REQUEST_CODE);
}
}
}
Activity C
public class SpotActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// Initialize actionbar and recyclerview adapter
// a click on a recyclerview's list item triggers this method
adapter.setSurfSpotSelectedListener(new SurfSpotSelectedListener() {
#Override
public void onSpotSelected(String spotName) {
Intent intent = new Intent();
intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, spotName);
setResult(RESULT_OK, intent);
finish();
}
});
}
}
What does navigate back to activity C mean?If you dont start C By startActivityForResult,you will get no result in B.
You should set your result by overriding OnBackPressed(), for example:
#Override
public void onBackPressed() {
setResult(111, new Intent().putExtra("result", "from c"));
super.onBackPressed();
}
For Actionbar, you should overriding onOptionsItemSelected(MenuItem item)
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
//here
setResult(111, new Intent().putExtra("result", "from c"));
this.finish();
default:
return super.onOptionsItemSelected(item);
}
}
I ended up solving it by setting the setNavigationOnClickListener for the toolbar in Acitvity B andActivity C and calling onBackPressed() to mimic back navigation. Thanks for all your help Tiny Sunlight and Good Try. Here is my Activity B & C. I hope this helps others.
Activity B
public class StateActivity extends AppCompatActivity {
#Bind(R.id.toolBar) Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spot_selection);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
// Recyclerview and Adapter left out for brevity
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == AlertCreationActivity.ALERT_CREATION_REQUEST_CODE) {
Intent intent = new Intent();
intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, data.getStringExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA));
setResult(RESULT_OK, intent);
finish();
}
}
}
Activity C
public class SpotActivity extends AppCompatActivity {
#Bind(R.id.toolBar) Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_spot_selection);
ButterKnife.bind(this);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
// Setting up Recyclerview and Adapter omitted
adapter.setSurfSpotSelectedListener(new SurfSpotSelectedListener() {
#Override
public void onSpotSelected(String spotName) {
Intent intent = new Intent();
intent.putExtra(AlertCreationActivity.ALERT_CREATION_REQUEST_DATA, spotName);
setResult(RESULT_OK, intent);
finish();
}
});
}
}

Categories