onactivityresult() is never called - java

I am trying to send mail from my app.The problem is after successful/unsuccessful delivery of the email it doesn't return to the activity, meaning that onActivityResult() is not being called.
Here is my code:
String[] recipients = {"soham#gmail.com"};
Intent email = new Intent(Intent.ACTION_SEND, Uri.parse("mailto:"));
// prompts email clients only
email.setType("message/rfc822");
email.putExtra(Intent.EXTRA_EMAIL, recipients);
try{
// the user can choose the email client
startActivityForResult(Intent.createChooser(email, "Choose an email client from..."), 1);
}catch(android.content.ActivityNotFoundException ex){
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if(requestCode == 1){
if(resultCode == RESULT_OK){
}else if (resultCode == RESULT_CANCELED){
}
}
}
I have checked this but not working for me. Can anyone tell me what am I doing wrong.
Edit
I got the problem.It will work fine in Activity.But it will not work on Fragment or FragmentActivity. All fragment is closing down forcefully.You can say my app is going on the background.How to solve this issue?Anybody got any idea.

If i uderstand your problem correctly;
In your activity's onActivityResult part you can find your fragment
YourFragment fragment = (YourFragment)getSupportFragmentManager().findFragmentById(R.id.your_framelayout_id);
And use your fragment's public method:
if(fragment != null)
{
fragment.yourPublicMethod();
}
In yourPublicMethod you can do whatever you want. I hope this helps you.

Related

UPI integration in android: How to use onActivityResult?

UPI (Unified Payment Interface) is a payment interface for Indian banks.
In UPI transactions are links. Just like bitcoin transactions are messages
Those links are passed to UPI payment apps and the payer has to login to the app and click the pay button.
Our app has to start an intent and pass link to the UPI payment app and after payer clicks the pay button we need to call onActivityResult.
I dont know anything about android development in java.
I use python kivy for android development. I want to know what should my onActivityResult should do.
Sample code :
UPI App Deep linking using Intent - inconsistent and buggy behavior
I can use java code in python using pyjnius.
Some reference link:
https://blog.deazzle.in/enable-upi-payments-in-your-app-without-the-need-to-integrate-with-a-bank-c911019f3b2d
you have not any need to do it manually. I have developed a library for it.
Just have to do a simple process.
final EasyUpiPayment easyUpiPayment = new EasyUpiPayment.Builder()
.with(this)
.setPayeeVpa("EXAMPLE#VPA")
.setPayeeName("PAYEE_NAME")
.setTransactionId("UNIQUE_TRANSACTION_ID")
.setTransactionRefId("UNIQUE_TRANSACTION_REF_ID")
.setDescription("DESCRIPTION_OR_SMALL_NOT")
.setAmount("AMOUNT_IN_DECIMAL_XX.XX")
.build();
easyUpiPayment.startPayment();
For more info, you can visit below site.
https://github.com/PatilShreyas/EasyUpiPayment-Android
Activity A:
Intent start = new Intent(MainActivity.this, PurchaseActivity.class);
startActivityForResult(start, 1);
And add this result listener:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
//payment was successful
}else if (resultCode == RESULT_CANCELED) {
//payment was canceled
}
}
}
And Activity B:
If payment was successful:
setResult(RESULT_OK, new Intent());
finish();
or if it was canceled:
setResult(RESULT_CANCELED, new Intent());
finish();

StartActivityForResult go back

I am using StartActivityForResult for multiple activities. My main activity is where I initialize it. On my second activity I input some values and pass to a third activity. Now, When i'm on the third activity I want to be able to go back to the second activity if ever I want to edit the values i passed. What should I do?
MainAct.java
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
//Something
}
}
SecondAct.java
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish();
ThirdAct.java
Intent intent = new Intent();
intent.putExtra("firstname",name);
intent.putExtra("lastname", lname);
intent.putExtra("licensenumber", lnumber);
setResult(Activity.RESULT_OK, intent);
finish();
How can I go back to second Activity from third activity to edit some values if ever?
You should not call finish() on second activity when starting the third one.
Then onActivityResult() will be call when third activity is finished.
Call
startActivityForResult(vd2);
instead of
startActivity(vd2);
simply remove finish();
Intent vd2 = new Intent(ViolatorDetails1.this,ViolatorDetails2.class);
vd2.putExtra("name",name);
vd2.putExtra("lname",lname);
vd2.putExtra("lnumber",lnumber);
vd2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
vd2.addFlags(Intent.FLAG_ACTIVITY_FORWARD_RESULT);
startActivity(vd2);
finish(); //remove this line
in this way when your third activity closes user will go back to 2nd one, also you should implement onActivityResult in your 2nd activity so you can handle weather user wants to edit or has finished and should go back to 1st activity! (i.e. setting the result of the intent came from 1st activity and finish the 2nd one!)
here is what I mean in code:
In your 2nd activity do this,
#override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE)
{
if (resultCode == Activity.RESULT_OK)
{
// user should have done his job on 3rd activity and we should finish the 2nd activity to go back to first activity!
}else{
//user still editing!
}
}
and instead of startActivity(vd2); do startActivityForResult(vd2);

Android: cannot get values using .getStringExtra

I'll just go straight to the problem. In UploadNotesActivity.java....
First, I pick a .pdf file using intent
chooseNotesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Create intent to Open Image applications like Gallery, Google Photos
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
// Start the Intent
startActivityForResult(intent, RESULT_LOAD_FILE);
}
});
and then, in `onActivityResult, I save the filePath of the picked file.
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_FILE && resultCode == RESULT_OK && data != null) {
data.putExtra("filePath", data.getData().getPath());
choosenFile.setText(data.getStringExtra("filePath"));
} else {
Toast.makeText(this, "Error in choosing file",
Toast.LENGTH_LONG).show();
}
}
click Upload button to start upload the file
uploadNotesBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onUploadButtonClick();
}
});
the onUploadButtonClick()
private void onUploadButtonClick() {
final String serverUrlString = "http://XXXX/uploadNotes.php";
if (getIntent().getStringExtra("filePath").isEmpty()) {
Log.d(TAG, "filePath is null");
} else {
Log.d(TAG, getIntent().getStringExtra("filePath"));
}
final String fileToUploadPath = getIntent().getStringExtra("filePath");
final String paramNameString = "uploaded_file";
String fileName[] = fileToUploadPath.split("/");
final MultipartUploadRequest request =
new MultipartUploadRequest(this, UUID.randomUUID().toString(), serverUrlString);
request.addFileToUpload(fileToUploadPath, paramNameString,
fileName[fileName.length-1]+".pdf", ContentType.APPLICATION_OCTET_STREAM);
request.setNotificationConfig(R.drawable.ic_launcher,
getString(R.string.app_name),
getString(R.string.uploading),
getString(R.string.upload_success),
getString(R.string.upload_error),
false);
// if you comment the following line, the system default user-agent will be used
request.setCustomUserAgent("UploadServiceDemo/1.0");
// set the intent to perform when the user taps on the upload notification.
// currently tested only with intents that launches an activity
// if you comment this line, no action will be performed when the user taps
// on the notification
// request.setNotificationClickIntent(new Intent(this, MainActivity.class));
// set the maximum number of automatic upload retries on error
request.setMaxRetries(2);
try {
request.startUpload();
} catch (Exception exc) {
Toast toast = Toast.makeText(getApplicationContext(), "Malformed upload request. " + exc.getLocalizedMessage(), Toast.LENGTH_LONG);
toast.show();
}
}
But the problem is, it will throw null pointer exception, which I don't quite get the reason.
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.isEmpty()' on a null object reference
at com.fyp.mycyberlaw.Lecturer.UploadNotesActivity.onUploadButtonClick(UploadNotesActivity.java:73)
at com.fyp.mycyberlaw.Lecturer.UploadNotesActivity.access$100(UploadNotesActivity.java:19)
at com.fyp.mycyberlaw.Lecturer.UploadNotesActivity$2.onClick(UploadNotesActivity.java:53)
line 73: if (getIntent().getStringExtra("filePath").isEmpty())
line 19: public class UploadNotesActivity extends Activity
line 53: onUploadButtonClick();
Seems like the filePath in line 73 is empty and the way I save filePath into bundle (?) is incorrect. How to get the filePath from onActivityResult? Here's the .java class, just in case. Thank you in advance. Really need your help.
An Intentobject is used to pass params between activities. Ones you receives the file path you must to keep it in your activity.
Create a filePathvariable inside your activity, set it on onActivityResult and read it on onUploadButtonClick.
Notice that must save variable value during the onSaveInstanceState callback and restore it in onCreate because every time you turn your phone the activity is destroyed and recreated. Check this for more information: Recreating an Activity

retrieve value from second activity

In my MainActivity, I launch a second activity:
Button button = (Button) findViewById(R.id.btnPush);
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
Intent nowStart = new Intent(getApplicationContext(), AddPillScheduleActivity.class);
startActivityForResult(nowStart, RESULT_OK);
//startSecond();
}
});
Then inside my Second Activity, I would like to return a value back to the main activity.
Intent i=new Intent();
i.putExtra("ANSWER", ans);
setResult(RESULT_OK,i);
finish();
That seems to execute fine, but back in my MainActivity, I would like to grab the value. This is where I am having trouble. My debugger never stops on my onActivityResult, which is:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name = getIntent().getExtras().getString("ANSWER");
if (resultCode == RESULT_OK) {
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
Can someone shed a little light? Thanks.
You're not getting the value from the right intent, the one that comes with the method. Modify your code to the following:
#Override
protected void onActivityResult(int requestCode ,int resultCode ,Intent data ) {
super.onActivityResult(requestCode, resultCode, data);
String name = data.getStringExtra("ANSWER");
if (resultCode == RESULT_OK) {
Toast.makeText(this, name, Toast.LENGTH_LONG).show();
}
}
Use the getStringExtra method as you put a String in your intent and not a bundle. Also, not the best practice use the same code for the requestCode and resultCode.
String name = getIntent().getExtras().getString("ANSWER");
is accessing the wrong intent. getIntent() returns the intent that started the activity. You want
String name = data.getExtras().getString("ANSWER");
Also, the second parameter of startActivityForResult() is a request code, so using RESULT_OK -- althought it still works -- is confusing.

No idea what I'm doing with IntentIntegrator

I'm having trouble getting zxing to do anything after scanning a barcode. I call the zxing using:
IntentIntegrator.initiateScan(MainActivity.this);
And my onActivityResult looks like this
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
String contents = intent.getStringExtra("SCAN_RESULT");
String format = intent.getStringExtra("SCAN_RESULT_FORMAT");
if (format == "PRODUCT_CODE"){
//If the barcode scanned is of the correct type then pass the barcode into the search method to get the product details
Toast.makeText(getApplicationContext(),"You scanned " + contents, 3).show();
}
else{
//If the barcode is not of the correct type then display a notification
Toast.makeText(getApplicationContext(),"You didn't scan a product code", 3).show();
}
} else if (resultCode == RESULT_CANCELED) {
//If the scan is cancelled then display a notification
Toast.makeText(getApplicationContext(),"You cancelled the input", 3).show();
}
}
}
But whenever I exit zxing nothing is displayed. I tried using the example in the zxing wiki
but whenever I try to replace yourActivity with MainActivity or MainActivity.this I receive errors (I get told MainActivity cannot be resolved to a string and with MainActivity.this that The constructor IntentIntegrator(MainActivity) is undefined).
Basically I have no idea what I'm doing and why it's not working. Any help would be greatly appreciated.
The problem is exactly the same as in your other question. You can't compare strings with ==, but must use equals().

Categories

Resources