Calling startActivity() from outside of an Activity context
Either
- cache the Context object via constructor in your adapter, or
- get it from your view.
Or as a last resort,
- add - FLAG_ACTIVITY_NEW_TASK flag to your intent:
_
myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Edit - i would avoid setting flags as it will interfere with normal flow of event and history stack.
Calling startActivity() from outside of an Activity
The problem is pretty obvious (and actually classic programming one :)) - you're setting your flag not to newly created intent1
, but to intent
, which is being passed as a parameter. So you just have to change this to:
intent1.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
And then it should work.
Intent from adapter: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
If you are using your Adapter in your Activity then simply use:
RecyclerView.Adapter<EntitiesListAdapter.MyViewHolder> mAdapter = new EntitiesListAdapter(entities, MainActivity.this);
If you want to use getApplicationContext()
, then you have to add FLAG_ACTIVITY_NEW_TASK
to the startActivity
’s Intent like:
Intent intent = new Intent(mContext, ViewEntityActivity.class);
intent.put("entity", row);
intent.setFlags(FLAG_ACTIVITY_NEW_TASK)
mContext.startActivity(intent);
Calling startActivity() from outside of an activity context requires the FLAG_ACTIVITY_NEW_TASK
Try this.
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
shareIntent.setType("text/plain");
shareIntent.putExtra(Intent.EXTRA_TEXT, bundle.getString("URL"));
Intent new_intent = Intent.createChooser(shareIntent, "Share via");
new_intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(new_intent);
Using Intent.createChooser and getting error: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag
Intent.createChooser
creates an Intent
, so you need to set the FLAG_ACTIVITY_NEW_TASK
flag on that intent, e.g.,
Intent i = new Intent(Intent.ACTION_SEND);
i.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
i.putExtra(Intent.EXTRA_STREAM, rasta); //rasta -> Uri obj
i.setType("image/*");
Intent chooserIntent = Intent.createChooser(i,"Share karna...");
chooserIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
getApplicationContext().startActivity(chooserIntent);
You were also calling startService
instead of startActivity
- make sure to correct that as well.
Related Topics
What Should I Use Android Accountmanager For
How to Set Android Layout to Support All Screen Sizes
How to Hide Status Bar in Android
Service Won't Stop When Stopservice Method Is Called
How to Create a User-Defined Function in SQLite
About Android Image and Asset Sizes
How to Load External Webpage in Webview
"Failed to Install the Following Android Sdk Packages as Some Licences Have Not Been Accepted" Error
Lock Screen Orientation (Android)
Why Are There Two Build.Gradle Files in an Android Studio Project
How to Detect When Phone Is Answered or Rejected
Android Emulator Not Able to Access the Internet
How to Draw Free Hand Polygon in Google Map V2 in Android