How to start an intent that extends fragment, from

2020-03-31 05:24发布

I have problem starting an intent that extends Fragment, from a class that extends Activity.

TabBar2.class --> extends Activity

Favourite.class --> extends Fragment

This is how I write my intent.

Intent intent1 = new Intent(TabBar2.this, Favourite.class);
startActivity(intent1);

But my app crashes when I run the intent when onClick. My logCat says

Unable to instantiate activity
 ComponentInfo{com.honey.test/com.honey.test.Favourite}:
 java.lang.ClassCastException: com.honey.test.Favourite

What did I do wrong? Can someone guide me on how to solve this?

1条回答
太酷不给撩
2楼-- · 2020-03-31 05:30

It is because you can't call Fragments via Intent, Fragment is a part of an FragmentActivity

All in all Fragment is a content not container, so you need to create a FragmentActivity and add Fragment(Favourite) in that, and then call

Intent intent1 = new Intent(TabBar2.this, SomeFragmentActivity.class);
startActivity(intent1);

A Fragment is a piece of an application's user interface or behavior that can be placed in an Activity more information

查看更多
登录 后发表回答