9

I have a fragment that has its own layout. In the layout, there is a listview and i attached onitemclick listener that will start/open an intent when the list row is clicked. Unfortunately, I always get this error :

Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag

But I do not prefer to set the flag activity. I could not open the SamplePage.class

public class FrontPageFragment extends Fragment { private ArrayList<Order> m_orders = null; private OrderAdapter m_adapter; @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View fragmentView=inflater.inflate(R.layout.frontpage, container, false); m_orders = new ArrayList<Order>(); this.m_adapter = new OrderAdapter(this.getActivity().getApplicationContext(), R.layout.name, m_orders); View eView=(View)fragmentView.findViewById(R.id.approvedOrders); ListView listView=(ListView)eView.findViewById(R.id.listview); listView.setOnItemClickListener(new OnItemClickListener() { public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Intent intent = new Intent(view.getContext(), SamplePage.class); intent.putExtra("id", "1"); view.getContext().startActivity(intent); } }); //bind data listView.setAdapter(this.m_adapter); return fragmentView; } 
1
  • You need activity context from where you call this fragment. Instead starting new intent you can add another fragment which will be nearly the same. Commented Aug 24, 2011 at 15:03

2 Answers 2

12

When I copied ur code I got the same error. So I changed it a little bit, and it worked. Check this:

Intent intent = new Intent(getActivity().getBaseContext(), Sampleclass.class); intent.putExtra("id", 1); startActivity(intent); 
Sign up to request clarification or add additional context in comments.

2 Comments

startActivity(intent); was the key! When I used other statements such as getBaseContext.startActivity(intent); then there were errors.
In my case the first line looks like Intent intent = new Intent(getBaseContext(), Sampleclass.class);
1

Try this:

FrontPageFragment.this.startActivity(intent); 

1 Comment

I solve my problem with your solution, thanks! I'm trying to start NewsDetailActivity activity inside onItemClick() inner class' method of NewsActivity activity with the following code: Intent i = new Intent(getApplicationContext(), NewsDetailActivity.class); i.putExtra("newsId", "1"); NewsActivity.this.startActivity(i);

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.