How to get Callback share result in android - java

How to get the intent callback sharing result, i have seen some code and implemented from my side. Below is the sample code
try {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Body");
startActivityForResult(Intent.createChooser(intent, "Choose"), 1);
} catch (Exception e) {
e.printStackTrace();
}
Code for callback result
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if (resultCode == RESULT_OK) {
}
}
}
In this requestCode is always getting 0 not 1 after i successfully share to any social media platform, is there any code which is newer version?

The method you're using is deprecated, The new way is
ActivityResultLauncher<Intent> launcher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent data = result.getData();
// do something
}
});
Launching the activity
launcher.launch(yourIntent);
sharing result
Intent intent = new Intent();
intent.putExtra(YOUR_KEY, YOUR_DATA);
setResult(RESULT_OK, intent);
Dependency
dependencies {
implementation 'androidx.appcompat:appcompat:1.4.2'
implementation "androidx.activity:activity:1.2.0"
...
}
To learn more about this visit

Related

OnActivityResult() returning null data

I know this question has been asked many times and i followed all the steps correctly but still getting null data in my FirstApplication
FirstApplication starting an intent to get result from a library class using Mainactivity that is initialised using the activity variable in a constructor:
sendIntent.putExtra("data", bundle);
sendIntent.setType("text/plain");
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
if (sendIntent.resolveActivity(context.getPackageManager()) != null) {
activity.startActivityForResult(sendIntent, requestCode);
}
SecondApplication getting the intent and doing necessary operation
Intent intent = getIntent();
Bundle bundle = intent.getBundleExtra("data");
if (bundle!=null){
type = bundle.getString("type");
num1 = Integer.parseInt(bundle.getString("num1"));
num2 = Integer.parseInt(bundle.getString("num2"));
if (type.equalsIgnoreCase("add")){
result = String.valueOf(num1+num2);
}else {
result = String.valueOf(num1-num2);
}
}
Sending result to FirstApplication
Bundle bundle1 = new Bundle();
if (bundle1!=null){
Intent send = new Intent();
bundle1.putString("result", result);
send.putExtra("datasend", bundle1);
setResult(Activity.RESULT_OK, send);
finish();
}
Receiving result in FirstApplication MAinActivity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
Bundle bundle = data.getBundleExtra("datasend");
result = bundle.getString("result");
}
} else {
super.onActivityResult(requestCode, resultCode, data);
}
}
But here i am receiving resultCode 0 and data null. Any help will be appreciated. Thank you.
The problem is in this line
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
By calling Intent.FLAG_ACTIVITY_NEW_TASK you are setting the second activity on the top of the back stack and cancelling the effect of startActivityForResult()
So it should be removed to get back the result

how to pick a file in SD card?

I'm trying to make an app with Android Studio that can select a file in SD card and get its path, like an OpenFileDialog, I've tried this:
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("file/*");
startActivityForResult(intent, PICKFILE_REQUEST_CODE);
However, it does not work, how can I do it ?
Try this:
Intent mediaIntent = new Intent(Intent.ACTION_GET_CONTENT);
mediaIntent.setType("*/*"); //set mime type as per requirement
startActivityForResult(mediaIntent,0);
You can change type according to your need.
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0
&& resultCode == Activity.RESULT_OK) {
Uri uri = data.getData();
Log.d("", "Video URI= " + videoUri);
}
}

Four Activities don't display Toast

I have four activities:
Activity A
private void addCard() {
Intent intent = new Intent(MainActivity.this, GetNumberActivity.class);
startActivityForResult(intent, REQUEST_CODE_CREATE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE_CREATE) {
if (resultCode == RESULT_OK) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
}
Activity B
Intent intent = new Intent(GetNumberActivity.this, ScanQrCodeActivity.class);
intent.putExtra(EXTRA_MESSAGE, uunitValue);
startActivityForResult(intent, REQUEST_CODE);
Then in the second activity I have to pass data to the third activity.
Activity C
Card card = new Card(path3, base32, nameCard, intervalTotp, passwordHotp, getDate(), expirationDate, hotpValue);
Intent intent = new Intent(ScanQrCodeActivity.this, Stage3Activity.class);
intent.putExtra("card", card);
startActivity(intent);
finish();
Activity D
Intent data = new Intent(Stage3Activity.this,MainActivity.class);
data.putExtra("data", card);
startActivityForResult(data, RESULT_OK);
When I press the button on Activity A, the Toast is not shown.
You need to update your code as follow
if (resultCode == RESULT_OK) {
if (requestCode == REQUEST_CODE_CREATE) {
if (data.hasExtra("data")) {
// Card has been create
Toast.makeText(getApplication(), "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
}
}
}
First check for RESULT_OK and then proceed further
Happy Coding!
Replace getApplication() with this (the context of the current activity)
Toast.makeText(this.class, "Karata została wygenerowana.", Toast.LENGTH_SHORT).show();
Use getApplicationContext() instead of getApplication() in your makeText() method

Call method when intent closes

I am making a music player application, and I am trying to implement playlists. I have a file chooser in another intent, and I would like the ListView in the mainActivity to update when the file chooser intent closes. how can I call my UpdateListView method when it closes?
start intent:
Intent intent = new Intent(this, FileChooser.class);
startActivity(intent);
Closing intent
public void closeButton(View view){
finish();
}
Any help would be appreciated! thanks!
I assume you are using your own FileChoser class, not a standard Android one:
private static final int FileChooserRequestCode = 666;
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FileChooserRequestCode);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == FillChooserRequestCode) {
if (resultCode == Activity.RESULT_OK) {
// ... file is chosen
String fileName = data.getStringExtra("FileName");
} else {
... dialog is closed
}
}
}
in FileChoser you do
Intent intent = new Intent();
intent.putStringExtra("FileName", fileName);
SetResult(Activity.RESULT_OK, intent);
finish();
and
SetResult(Activity.RESULT_CANCELED);
finish();
You can use startActivityForResult() please refer the link Getting Results From Activity
static final int FILE_CHOOSER_INTENT = 1; // The request code
...
private void chooseFile() {
Intent intent = new Intent(this, FileChooser.class);
startActivityForResult(intent, FILE_CHOOSER_INTENT);
}
Call setResult pass your result data as Intent. for details refer link SetResult function
Override this in your calling activity
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == FILE_CHOOSER_INTENT) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// The user picked a contact.
// The Intent's data Uri identifies which contact was selected.
// Do something with the contact here (bigger example below)
}
}
}

Android onActivityResult() doesn't get called

I've got a problem I don't know how to solve.
This is my code of the MainActivity.java which starts a new activity:
Intent intent = new Intent(this, AssociationActivity.class);
intent.putExtra("data", json);
startActivityForResult(intent, 0);
Additionaly, I have the following onActivityResult()-method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 1) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}
And this is where the result should be sent to the MainActivity:
Intent returnIntent = new Intent();
returnIntent.putExtra("result", string);
setResult(RESULT_OK, returnIntent);
finish();
It seems that the onActivityResult() doesn't even get called as I don't have any output on the console.
In the AndroidManifest.xml I neither use the singleIntent nor the noHistory parameter.
Any suggestions why it doesn't work?
requestCode change from 1 to 0. as shown below
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
System.out.print("works outside!");
if (requestCode == 0) {
if(resultCode == RESULT_OK){
System.out.print("works inside!");
}
if (resultCode == RESULT_CANCELED) {
}
}
}
Okay, I found the issue, the wasn't any issue at all, I replaced the System.out statements with the Log from Android it's working all the time, Android just delayed the System.out, but I don't know why.
Thanks #Rene Juuse for that tip!

Categories

Resources