I am trying to call a class whıch extends fragment from an activity class with startActivity()
. It returns me cannot be cast to android.app.activity error. I am confused how can I open fragment vıew from activity.
Asked
Active
Viewed 110 times
0

user3834286
- 13
- 1
-
You can not open fragment from an activity but you have to call a fragment using fragment manager and fragment transaction. – madteapot Jul 13 '14 at 13:28
-
but I want to call a class which extends fragment. can fragment manager do this? – user3834286 Jul 13 '14 at 13:30
-
You question could do with a more descriptive title. – Kris Jul 13 '14 at 13:43
1 Answers
1
In the activity, you create an transaction to replace a fragment by another:
FragmentSelectDate myFragment = new FragmentSelectDate();
myFragment.setArguments(getIntent().getExtras());
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.replace(R.id.containerMain, myFragment,
"FragmentSelectDate");
transaction.addToBackStack(null);
transaction.commit();

roberth
- 924
- 9
- 17
-
-
both getsupportfragmentmanager and getfragmentmanager cannot be resolved sınce my first class extends activity. also what is R.id.containerMain ? – user3834286 Jul 13 '14 at 13:39
-
@user3834286, the activity where you use getSupportFragmentManager has to extend "FragmentActivity". R.id.containerMain is a
, that I placed in the Activity's layout (.XML). So the Activity has a layout (.XML), that holds a FrameLayout (the container for the fragment). You can have more fragments in 1 activity-layout) – roberth Jul 14 '14 at 14:42