56

I have a set of tabs inside of a FragmentActivity that each hold their own fragment. When I tried to start a new activity from within that fragment via an onClickListener, and using the startActivity(myIntent) method, my application force closes.

After looking around for a while, I found a reference or two to a method called startActivityFromFragment, but after searching around for an hour or so I can't find any explanations or examples of how to use it or whether this is what I should be using.

I guess what I'm asking is whether there is any difference between launching a new activity from an activity, and launching a new activity from a fragment, and if so, what do I need to implement?

2
  • 1
    Post the code and the logcat output for when the app force closes
    – Squonk
    Aug 22, 2012 at 14:05
  • 1
    FragmentActivity.startActivityFromFragment() is called by Fragment.startActivityForResult() to implement its behavior, and Activity.startActivityFromFragment() is called when a Fragment in this activity calls its startActivity(). We don't need to call them directly. Dec 25, 2014 at 12:05

5 Answers 5

50

You should do it with getActivity().startActivity(myIntent)

5
  • I actually just tried that on a hunch - within the onClickListener I have an intent set up, and then I call the getActivity().startActivity(myIntent) - do I have to add anything else in particular to end the fragment or activity?
    – user1617134
    Aug 22, 2012 at 13:55
  • 7
    @EkKoZ : This is not necessary. The startActivity(Intent intent) method of Fragment effectively does the same thing.
    – Squonk
    Aug 22, 2012 at 14:02
  • that´s for sure, but i thought it could be an scope problem with the context
    – geekkoz
    Aug 22, 2012 at 14:05
  • 1
    not working showing blank screen.activity onCreate() method is calling from its showing a simple Blank screen. Mar 11, 2013 at 14:14
  • Make sure though that getActivity() doesnt return null because a Fragment is not attached to the Activity at it's birth Apr 7, 2014 at 22:54
45

I done it, below code is working for me....

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.hello_world, container, false);

        Button newPage = (Button)v.findViewById(R.id.click);
        newPage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), HomeActivity.class);
                startActivity(intent);
            }
        });
        return v;
    }

and Please make sure that your destination activity should be register in Manifest.xml file,

but in my case all tabs are not shown in HomeActivity, is any solution for that ?

2
  • it works for me now .. but I don't understand how it works :-( .. why cant I just use onClick method directly ? ..
    – McLan
    Nov 13, 2013 at 12:11
  • how can i use a backstack here Mar 6, 2022 at 7:36
13

The difference between starting an Activity from a Fragment and an Activity is how you get the context, because in both cases it has to be an activity.

From an activity: The context is the current activity (this)

Intent intent = new Intent(this, NewActivity.class);
startActivity(intent);

From a fragment: The context is the parent activity (getActivity()). Notice, that the fragment itself can start the activity via startActivity(), this is not necessary to be done from the activity.

Intent intent = new Intent(getActivity(), NewActivity.class);
startActivity(intent);
4
  • What if I have a fragmentactivity?
    – Euridice01
    Jul 14, 2019 at 14:01
  • @Euridice01 BecauseFragmentActivity extends Activity, you can use this as the context, just like from a normal activity. More info can be found here: stackoverflow.com/a/39733562 Jul 15, 2019 at 11:13
  • One more thing, what about BindingFragmentActivity?I can't find anything on that online.
    – Euridice01
    Jul 15, 2019 at 12:42
  • @Euridice01 If you mean BindingFragmentActivity from the androidbound library, then BindingFragmentActivity extends FragmentActivity, which, as mentioned above extends Activity. I have never used the androidbound library, but from what it looks like, BindingFragmentActivity isn't supported anymore (don't quote me on that). Jul 16, 2019 at 14:41
6

I do it like this, to launch the SendFreeTextActivity from a (custom) menu fragment that appears in multiple activities:

In the MenuFragment class:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_menu, container, false);

    final Button sendFreeTextButton = (Button) view.findViewById(R.id.sendFreeTextButton);
    sendFreeTextButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Log.d(TAG, "sendFreeTextButton clicked");
            Intent intent = new Intent(getActivity(), SendFreeTextActivity.class);
            MenuFragment.this.startActivity(intent);
        }
    });
    ...
1

Use the Base Context of the Activity in which your fragment resides to start an Intent.

Intent j = new Intent(fBaseCtx, NewactivityName.class);         
startActivity(j);

where fBaseCtx is BaseContext of your current activity. You can get it as fBaseCtx = getBaseContext();