Issue
Reaching out to ask for your help on the following.
I have a RecyclerView and in my adapter I'm trying to start a new activity. But I'm getting the following error:
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
Here is my code:
private Context mContext;
...
public EntitiesListAdapter(ArrayList<Entity> myDataset, Context context) {
mDataset = myDataset;
mContext = context;
}
...
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mContext, ViewEntityActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("entity", row);
mContext.startActivity(intent);
}
});
And I'm setting the adapter this way:
RecyclerView.Adapter<EntitiesListAdapter.MyViewHolder> mAdapter = new EntitiesListAdapter(entities, getApplicationContext());
But already tried like private Context context
and using context
instead of getApplicationContext()
.
Do you identify what I'm missing?
Thank you in advance.
Solution
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);
Answered By - DarShan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.