how to start new activity for share action from CardView - java

I am trying to give the user the option to share cardview content.
i use this code in the main activity and it is working fine:
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = "some text to share";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, "Share via"));
but when i use the same code from recyclerview i get error and the application crashed.
also i modified the code by adding FLAG and still not working
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
String shareBody = " teext to be shared";
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Subject Here");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
sharingIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(Intent.createChooser(sharingIntent, "Share via"));
iam getting this error:
Calling startActivity() from outside of an Activity context requires the
FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
holder.iconFavorites_btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// iam calling the code here and this block of code is located inside adapter class for recycleview .... the code located inside onBindViewHolder mithod
}
});
any body have solution for this problem ? if any information needed just tell me

In order to start an activity outside of another activity, you must use the FLAG_ACTIVITY_NEW_TASK flag for the intent (as you posted).
You would set this via sharingIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

Related

startActivityForResult() is deprecated, use registerForActivityResult() [duplicate]

My app contains a simple Fragment that is used to open external web pages, with:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivityForResult(chooserIntent, RC_OPEN_URL);
And, when the result is returned (in my Fragment's onActivityResult(...)), the Fragment is popped off the backstack.
But, I'm now getting the new deprecation warning:
startActivityForResult(Intent,int) in Fragment has been deprecated
I've read the corresponding Getting a result from an activity documentation, but I'm not sure how to adapt the example they provide for my particular case.
I have discovered the ActivityResultContracts.StartActivityForResult class, but can't figure out how to pass my chooserIntent to it.
All the online examples for the class seem to be in Kotlin and I've not had any joy trying to decompile them to Java. So a Java example of how to use the new registerForActivityResult() method to open an external URL would be much appreciated.
There's no reason to use startActivityForResult() at all for createChooser() - you can use startActivity and run your code from onActivityResult() immediately after you call to startActivity:
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
startActivity(chooserIntent);
// Do your code from onActivityResult() here
However, if you really want to use the Activity Result API, then you can directly adapt the examples in the documentation, replacing the example GetContent contract with the StartActivityForResult contract:
// Create this as a variable in your Fragment class
ActivityResultLauncher<Intent> mLauncher = registerForActivityResult(
new StartActivityForResult(),
new ActivityResultCallback<ActivityResult>() {
#Override
public void onActivityResult(ActivityResult result) {
// Do your code from onActivityResult
}
});
private void triggerChooser(Uri externalUri) {
Intent intent = new Intent(Intent.ACTION_VIEW, externalUrl); // Uri
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Intent chooserIntent = Intent.createChooser(intent, "Open URL...");
mLauncher.launch(chooserIntent);
}
Below answer may help to someone.... But this is not readymade solution to above question.
I have faced lot of issue when I try to get result from activity to fragment.
Finally I found below solution.
In side the fragment, I created ActivityResultLauncher.
var myActivityResultLauncher: ActivityResultLauncher<Intent> = registerForActivityResult<Intent, ActivityResult>(
ActivityResultContracts.StartActivityForResult(),
ActivityResultCallback<ActivityResult> {
// ToDO:
if (it.resultCode == AppCompatActivity.RESULT_OK) {
}
}
) as ActivityResultLauncher<Intent>
And when I start the activity I used below code.
myActivityResultLauncher.launch(myIntent)

android simulator crashes when using intent

I'm new to android studio, I'm using java to write my application.
I found that when I use intent to make the page jump from a page call PhotosActivity to another page call AndroidTabLayoutActivity was failed,I have no idea with what is going on.The android simulator does't give me any error massage and it just close the application automatically.
the code of calling intent:
if (!error) {
Toast.makeText(getApplicationContext(), "Offer successfully inserted.", Toast.LENGTH_LONG).show();
// Jump to the AndroidTabLayoutActivity page
Intent intent = new Intent(
PhotosActivity.this,
AndroidTabLayoutActivity.class);
startActivity(intent);
finish();
}
The code of AndroidTabLayoutActivity:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TabHost tabHost = getTabHost();
// Tab for Photos
TabSpec photospec = tabHost.newTabSpec("Photos");
photospec.setIndicator("Photos", getResources().getDrawable(R.drawable.icon_photos_tab));
Intent photosIntent = new Intent(this, PhotosActivity.class);
photospec.setContent(photosIntent);
//there is still some others tab in here
// Adding all TabSpec to TabHost
tabHost.addTab(photospec); // Adding photos tab
}
You have using getApplicationContext() in this toast method
Replace with activity context, i am sure your code is working fine
Toast.makeText(getApplicationContext(), "Offer successfully inserted.",
Toast.LENGTH_LONG).show();
Use like this
Toast.makeText(PhotosActivity.this, "Offer successfully inserted.",
Toast.LENGTH_LONG).show();
As there is no any particular error log. We have to backtrack all the possibilities .
First of all replace your toast with this.
Toast.makeText(getApplicationContext(), "Offer successfully inserted.", Toast.LENGTH_LONG).show();
After that ensure that you've declared your AndroidTabLayoutActivity in your AndroidManifest.xml file
In your AndroidTabLayoutActivity Cooment out all your code and check its working ? if yes than problem is in AndroidTabLayoutActivity. reply me with whats working for you

NullPointerException with Sharing Intent in Android Nougat

Currently, I'm trying to create a Sharing Intent to be called in the Android application itself when you click a "share" button to share an image. However, with the change to how Sharing Intents function in Android Nougat, I am getting a NullPointerException on this line:
Uri uImageFile = FileProvider.getUriForFile(DetailActivity.this, DetailActivity.this.getApplicationContext().getPackageName(), imageFile);
This was the error that I received:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.XmlResourceParser android.content.pm.ProviderInfo.loadXmlMetaData(android.content.pm.PackageManager, java.lang.String)' on a null object reference
at android.support.v4.content.FileProvider.parsePathStrategy(FileProvider.java:584)
at android.support.v4.content.FileProvider.getPathStrategy(FileProvider.java:558)
at android.support.v4.content.FileProvider.getUriForFile(FileProvider.java:400)
Here's the full code for my Share Intent method:
private void shareIntent() {
Intent sharingIntent = new Intent(Intent.ACTION_SEND);
sharingIntent.setType("image/jpg");
File imageFile = new File(data.get(pos).getUrl());
Uri uImageFile = FileProvider.getUriForFile(DetailActivity.this, DetailActivity.this.getApplicationContext().getPackageName(), imageFile);
sharingIntent.putExtra(Intent.EXTRA_STREAM, uImageFile);
startActivity(Intent.createChooser(sharingIntent, getResources().getText(R.string.share_to)));
}
data.get(pos).getUrl() returns the URL of a custom class I made that implements Parcelable, and when printing it out, it returns a directory like the following:
"/storage/emulated/0/Pictures/primitive/Primitive-79538313.jpg"
Thank you for any help that you can provide!
Change your code to this
private void shareIntent() {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("image/jepg");
shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://"+data.get(pos).getUrl()));
startActivity(Intent.createChooser(shareIntent, "Share image"));
}
Add this code in your activity onCreate()
StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

No Apps can perform this action android

I'm trying to make this program that when the user clicks a button, a message is sent to a whatsapp number
Here is the code in the onClick method
Uri uri = Uri.parse("smsto:" + "xxxxxxxxxx"); //xxx.. is the mobile number
Intent i = new Intent(Intent.ACTION_SENDTO, uri);
i.putExtra(Intent.EXTRA_TEXT, "Check");
i.setType("text/plain");
i.setPackage("com.whatsapp");
startActivity(Intent.createChooser(i, ""));
It shows that there are no apps that can perform this action. Why?
I removed
i.setType("text/plain");
And it works. But the text "Check" is not sent. How to do that if this is not the way.
Your format is slightly different from the example given on the WhatsApp FAQ, so I would modify to match.
From this page:
Like most social apps on Android, WhatsApp listens to intents to share media and text. Simply create an intent to share text, for example, and WhatsApp will be displayed by the system picker:
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
sendIntent.setType("text/plain");
startActivity(sendIntent);
However, if you prefer to share directly to WhatsApp and bypass the system picker, you can do so by using setPackage in your intent:
sendIntent.setPackage("com.whatsapp");
This would simply be set right before you call startActivity(sendIntent);
Try this code to send ON whatsapp
public void SendWhatsapp(View view) {
PackageManager pm=getPackageManager();
try {
Intent waIntent = new Intent(Intent.ACTION_SEND);
waIntent.setType("text/plain");
String text = "YOUR TEXT HERE";
PackageInfo info=pm.getPackageInfo("com.whatsapp", PackageManager.GET_META_DATA);
//Check if package exists or not. If not then code
//in catch block will be called
waIntent.setPackage("com.whatsapp");
waIntent.putExtra(Intent.EXTRA_TEXT, text);
startActivity(Intent.createChooser(waIntent, "Share with"));
} catch (NameNotFoundException e) {
Toast.makeText(this, "WhatsApp not Installed", Toast.LENGTH_SHORT)
.show();
}
}

How to share text from android app to facebook?

I try to implement share button in my android app to share in facebook
The code:
facebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent share = new Intent(android.content.Intent.ACTION_SEND);
share.setType("text/plain");
share.putExtra(Intent.EXTRA_SUBJECT,desc.getText().toString());
startActivity(Intent.createChooser(share, "Share description using"));
}
});
The problem is, in the post dialog of the facebook not display the text which i need to share from my app which it is (desc.getText().toString()) . There empty message !! why the text didn't appear !!!
Thanks in advance
Try with this. Hope it help
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_TEXT, "Share Something");
startActivity(Intent.createChooser(intent, "Share with"));

Categories