1

I am getting the error AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. The full stack trace:

main Process: com.kd.book, PID: 3487 android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ContextImpl.startActivity(ContextImpl.java:1238) at android.app.ContextImpl.startActivity(ContextImpl.java:1225) at com.kd.book.Adapter.MyComicAdapter$1.onClick(MyComicAdapter.java:63) at com.kd.book.Adapter.MyComicAdapter$MyViewHolder.onClick(MyComicAdapter.java:99)

I am new to Android development and do not know how to solve it.

My Code:

public class MyComicAdapter extends RecyclerView.Adapter<MyComicAdapter.MyViewHolder> {
    Context context;
    List<Comic> comicList;
    LayoutInflater inflater;

    public MyComicAdapter(Context context, List<Comic> comicList) {
        this.context = context;
        this.comicList = comicList;
        inflater=LayoutInflater.from(context);
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        View itemView = inflater.inflate(R.layout.comic_item, viewGroup, false);
        return new MyViewHolder(itemView);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, final int i) {

        Picasso.get().load(comicList.get(i).Image).into(myViewHolder.comic_image);
        myViewHolder.comic_name.setText(comicList.get(i).Name);

        //Event

        myViewHolder.setRecyclerItemClickListener(new IRecyclerItemClickListener(){

            @Override
            public void onClick(View view, int position) {
                //save the comic selected
                Common.comicSelected = comicList.get(position);
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(new Intent(context,ChapterActivity.class));

            }
        });
    }

    @Override
    public int getItemCount() {
        return comicList.size();
    }

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        TextView comic_name;
        ImageView comic_image;

        IRecyclerItemClickListener recyclerItemClickListener;

        public void setRecyclerItemClickListener(IRecyclerItemClickListener recyclerItemClickListener) {
            this.recyclerItemClickListener = recyclerItemClickListener;
        }

        public MyViewHolder(@NonNull View itemView) {
            super(itemView);

            comic_image = itemView.findViewById(R.id.image_comic);
            comic_name = itemView.findViewById(R.id.comic_name);

            itemView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {

            recyclerItemClickListener.onClick(view, getAdapterPosition());
        }
    }
}

Any ideas?

Elletlar
  • 3,136
  • 7
  • 32
  • 38
  • here is my logcat detials FATAL EXCEPTION: main Process: com.kd.book, PID: 3487 android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want? at android.app.ContextImpl.startActivity(ContextImpl.java:1238) at android.app.ContextImpl.startActivity(ContextImpl.java:1225) at com.kd.book.Adapter.MyComicAdapter$1.onClick(MyComicAdapter.java:63) at com.kd.book.Adapter.MyComicAdapter$MyViewHolder.onClick(MyComicAdapter.java:99) – K.D Kamal Dhital Feb 23 '19 at 13:51
  • @K.DKamalDhital, use this `view.getContext().startActivity(new Intent(context,ChapterActivity.class));` – ॐ Rakesh Kumar Feb 23 '19 at 14:42
  • @K.DKamalDhital, Why did you created the `myViewHolder.setRecyclerItemClickListener` and what exactly you want to do?? can you explain that will help us to solve your issue. – ॐ Rakesh Kumar Feb 23 '19 at 14:44

3 Answers3

2

The problem is that you are doing "new Intent" twice:

  • Once here: Intent intent = new Intent(Intent.ACTION_VIEW);
  • Second time here: context.startActivity(new Intent(context,ChapterActivity.class))

FLAG_ACTIVITY_NEW_TASK is not set when the activity is started. The corrected version where the Intent is only created once looks like:

Intent intent = new Intent(context, ChapterActivity.class))
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);

But your real problem is that you need to call startActivity on the Activity context.

getActivity().startActivity(new Intent(getActivity(), ChapterActivity.class));

More discussion here: How do I start an activity from within a Fragment?

What you were attempting to do with FLAG_ACTIVITY_NEW_TASK should be reserved for rare cases like when you need to launch an Activity from a Service. But it is not necessary or desirable to set that flag when starting an Activity from within a UI context.

This is why the warnings asks:

Is this really what you want?

Setting specialised flags when launching the Activity often leads to unwanted backstack/navigation behaviours.

Also, I cannot see if your adapter is located inside an Activity or Fragment. [You can use "this" inside an Acitivity and getActivity() inside a Fragment]

Lastly, I cannot how you are creating the Adapter, but the way your code is currently organised you need to pass the Activity context into the Adapter for it to work. From a Fragment:

adapter = new MyComicAdapter(getActivity(), list);

From an Activity:

adapter = new MyComicAdapter(this, list);

This will most likely prevent the exception that you are getting.

Elletlar
  • 3,136
  • 7
  • 32
  • 38
0

Try this way,
1. create an interface in your adapter and pass the position to it (hope you know how to make and use an interface).
2. implement that interface to your activity class.
3. in its override method, you will get the position of a list.
4. by using that position, get the required data and call intent in it.
Done.

Viraj S
  • 390
  • 2
  • 12
  • public interface IRecyclerItemClickListener { void onClick(View view, int position); } – K.D Kamal Dhital Feb 23 '19 at 14:00
  • not working here is the main problem myViewHolder.setRecyclerItemClickListener(new IRecyclerItemClickListener(){ @Override public void onClick(View view, int position) { //save the comic selected Common.comicSelected = comicList.get(position); Intent intent = new Intent(Intent.ACTION_VIEW); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); context.startActivity(new Intent(context,ChapterActivity.class)); – K.D Kamal Dhital Feb 23 '19 at 14:02
  • Ok, in your startActivity, you need to pass an intent object and NOT newIntent. – Viraj S Feb 23 '19 at 14:07
0

Change your call to the new activity as follows,

Intent intent=new Intent(context, ChapterActivity.class);
Activity activity=(Activity) context;
activity.startActivity(intent);
Bijo Baby
  • 1
  • 1
  • 2
  • java.lang.ClassCastException: android.app.ContextImpl cannot be cast to android.app.Activity at com.kd.book.Adapter.MyComicAdapter$1.onClick(MyComicAdapter.java:61) at com.kd.book.Adapter.MyComicAdapter$MyViewHolder.onClick(MyComicAdapter.java:98) i got this problem after that code. – K.D Kamal Dhital Feb 23 '19 at 14:26