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?

  • 1
    Post the code and the logcat output for when the app force closes – Squonk Aug 22 '12 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. – naXa stands with Ukraine Dec 25 '14 at 12:05

5 Answers5

50

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

geekkoz
  • 3,654
  • 4
  • 21
  • 19
  • 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? –  Aug 22 '12 at 13:55
  • 7
    @EkKoZ : This is not necessary. The `startActivity(Intent intent)` method of `Fragment` effectively does the same thing. – Squonk Aug 22 '12 at 14:02
  • that´s for sure, but i thought it could be an scope problem with the context – geekkoz Aug 22 '12 at 14:05
  • 1
    not working showing blank screen.activity onCreate() method is calling from its showing a simple Blank screen. – Abhijit Chakra Mar 11 '13 at 14:14
  • Make sure though that getActivity() doesnt return null because a Fragment is not attached to the Activity at it's birth – Vikram Bodicherla Apr 07 '14 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 ?

Jayesh
  • 3,661
  • 10
  • 46
  • 76
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);
David Dostal
  • 711
  • 13
  • 17
  • What if I have a fragmentactivity? – Euridice01 Jul 14 '19 at 14:01
  • @Euridice01 Because`FragmentActivity` extends `Activity`, you can use `this` as the context, just like from a normal activity. More info can be found here: https://stackoverflow.com/a/39733562 – David Dostal Jul 15 '19 at 11:13
  • One more thing, what about BindingFragmentActivity?I can't find anything on that online. – Euridice01 Jul 15 '19 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). – David Dostal Jul 16 '19 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);
        }
    });
    ...
fadedbee
  • 42,671
  • 44
  • 178
  • 308
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();

M-Wajeeh
  • 17,204
  • 10
  • 66
  • 103
user2766004
  • 81
  • 1
  • 5