Calling Startactivity() from Outside of an Activity Context

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.

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);

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 flag. Is this really what you want?

The Context that you pass into the CurrencySelectorAdapter constructor should be the Activity that hosts this RecyclerView. Then, you will not get this error.



Related Topics



Leave a reply



Submit