K
K
kirchevsky2012-06-17 08:37:42
Android
kirchevsky, 2012-06-17 08:37:42

How to start an Activity from a non-Activity class?

Somehow, I didn’t google a sensible answer to this question, that’s why I decided to turn to the Habra community for advice, maybe someone will tell you the correct and concise version.
So there is a class:

public class SlideMenuFragment extends ListFragment {

And at some point there is a need to launch an Activity from this class, but the options listed below do not work:
public Context mContext;<br/>
// .....<br/>
Intent intent = new Intent(mContext, AboutActivity.class);<br/>
mContext.startActivity(intent);

Perhaps someone will tell the young and green how to solve this problem or point where to dig?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
serso, 2012-06-17
@serso

ListFragment has a getActivity() method:

final Activity a = getActivity();
Intent intent = new Intent(a, AboutActivity.class);
a.startActivity(intent);

K
kolybelkin, 2012-06-17
@kolybelkin

if you edit yours, then you can write like this -
Intent intent = new Intent(this, AboutActivity.class);
mContext.startActivity(intent);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question