What i have is left navigation menu that i have made using navigation drawer and it works just fine with fragments , and i have five buttons each one open a fragment but now i want each case to open an activity instead of fragment , and i have tried to do it using intent but it didn't work !!
here is my code :
private void displayView(int position) {
// update the main content by replacing fragments
android.app.Fragment fragment = null;
switch (position) {
case 0:
//fragment = new HomeFragment();
// Intent i=new Intent(MainActivity.this,MainActivity.class);
// startActivity(i);
break;
case 1:
//fragment = new FindPeopleFragment();
break;
case 2:
//fragment = new PhotosFragment();
break;
case 3:
//fragment = new CommunityFragment();
break;
case 4:
//fragment = new PagesFragment();
break;
case 5:
//fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
So what shall i change in this code so it can open an activity instead of fragment??? can anyone help me?
You need to change your function to load a class Fragment in some case and in other cases a class Activity
case 0:
activity = new MyActivity();
fragment = null;
break;
case 1:
fragment = new MyFragment();
activity = null;
break;
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, (android.app.Fragment) fragment).commit();
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}else {
if(activity != null) {
Intent i1 = new Intent(MainActivity.this, activity.getClass());
i1.putExtra(EXTRA_MESSAGE, position);
startActivity(i1);
setTitle(menutitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
Could you please post the Error from Logcat:
What i would check first is:
Are the Activities declared in the Manifest.xml as Activies?
Secondly check if you calling Activities with an Intent, and not Fragments
This is how you start Activities correctly.
Intent intent = new Intent(mActivity, class1); // class1 The class to open
startActivity(intent);
Related
I have a sliding menu with some Fragments, but i want to change it with Activities, is it possible?
private void replaceFragment(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new X();
break;
case 1:
fragment = new Y();
break;
case 2:
fragment = new Z();
break;
default:
fragment = new X();
break;
}
if (null != fragment) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.main_content, fragment);
transaction.addToBackStack(null);
transaction.commit();
}
I don't know how to change the last part of the code. --> FragmentManager and --> FragmentTransaction. Thanks!!
Yes you can use Activity in place of fragment, But
you have following challenges.
You need to implement sliding drawer on every activity, if you uses fragment then HomeActivity sliding drawer will be shown on all fragment
update your code like this if you want to implement activity
private void replaceFragment(int pos) {
Intent intent = null;
Context context = this;
switch (pos) {
case 0:
intent = new Intent(context, SecondActivity.class);
break;
case 1:
intent = new Intent(context, ThirdActivity.class);
break;
case 2:
intent = new Intent(context, FourthActivity.class);
break;
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
if (intent != null) {
startActivity(intent);
}
}
There is problem with my fragment, they are showed together, one on the second fragment. How to disapear, and only show one of them?
Definiton:
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
fr = new avc);
FragmentTransaction ft = ((TestingActivity)context).getFragmentManager().beginTransaction();
ft.replace(R.id.test, fr);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
});
And definiton of container below:
<FrameLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="match_parent" />
You can use some thing like this. Make a function for showing a fragment and call each time this function with different parameter.
eg. If You want to show "HomeFragment" then call displayView(0) and if you want to show "FindPeopleFragment" then call displayView(1)
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I have a code in which it opens five fragment class,but i have one case to call activity class in my project.
Can I change case 3 to call activity class instead of fragment class?
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
If your questions is whether you can call new MyActivity(), the answer is no. Only the Android framework can instantiate an Activity and it does this based on an Intent being used in a call to Context.startActivity().
If you mean by calling is starting the new activity, then small answer is YES, you can do that.
But if you mean to instantiate is then; No, you can't do that.
switch (position) {
case 0:
*******WRONG WAY*******
myAct = new MyActivity(); // Where MyActivity is extended from Activity
break;
case 1:
*******RIGHT WAY*******
Intent intent=new Intent(MyCurrentActivity.this, MyNewActivity.class);
startActivity(intent);
break;
}
This is one of the method() of Navigation Drawer activity...
Code Snippet:
/**
* Diplaying fragment view for selected nav drawer list item
**/
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new ViewFragment();
break;
case 1:
//here i should call the FragmentActivity
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// Log.e("MainActivity", "Error in creating fragment");
}
}
create intent with fragment activity and use startActivity() to launch the fragment Activity.
switch (position) {
case 0:
fragment = new ViewFragment();
break;
case 1:
//here i should call the FragmentActivity
Intent intent = new Intent(Context, FragmentActivity.class);
startActivity(intent);
default:
break;
}
But make sure that your FragmentActivity. In FragmentActivity you have to use fragment to display the content.
/**
* Diplaying fragment view for selected nav drawer list item
* */
private void displayView(int position) {
// update the main content by replacing fragments
FragmentActivity fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new HomeFragment();//LessonPlanFragment();
break;
case 2:
fragment = new HomeFragment();//GradeBookFragment();
break;
case 3:
fragment = new HomeFragment();//StudentFragment();
break;
case 4:
fragment = new HomeFragment();//ReportsFragment();
break;
case 5:
fragment = new HomeFragment();//AppSettingsFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction ft = fragmentManager.beginTransaction();
ft.replace(R.id.frame_container, fragment);
ft.commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
}
I have tried using the support library but nothing helped. The error message reads "het method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, FragmentActivity).
This won't work. You're trying to replace Fragment with FragmentActivity and this is impossible. You can only do that with Fragments. For clarification FragmetnActivity acts as host Activity for Fragments in support library.