How do I replace the fragmentactivity in the code below? - android

/**
* 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.

Related

Replace Fragment showing both fragments

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");
}
}

How to save state of fragment in navigation drawer?

I am using navigation drawer in my android app and when i am reselecting fragment it loads twice.
Here is my code
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
// if(fraghome!=0)
// {
// fraghome=0;
fragment = new HomeFragment();
// }
break;
case 1:
fragment = new BlogsFragment();
break;
case 2:
fragment = new NewsFragment();
break;
case 3:
fragment = new TransferFragment();
break;
case 4:
fragment = new FixturesFragment();
break;
// case 5:
// fragment = new BestXIFragment();
// break;
case 5:
fragment = new FeedFragment();
break;
case 6:
fragment = new TwitterFragment();
break;
case 7:
fragment = new FacebookFragment();
break;
case 8:
fragment = new BookmarkFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction ft = fragmentManager.beginTransaction();
ft.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");
}
}
In each of fragment i am using async task and when i am selecting fragment the async task started again and again
please help me
Normally in fragment, fragmens are destroyed, if it is not the current top fragment or the fragment just right or left to the current top fragment. So the asynctask is getting called again and again.
I assume you are getting some data using the asynctask to show in the fragment. So try to make the data source variable static and check null for the data source variable before executing asynctask. if the data source is not null then show the data. I have added some abstruct code for your understanding. For better understanding you can see this link. Hope it helps. Thanks.....
In each fragment.....
public class TabFragment extends Fragment{
private static DataSource ds = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/*
.
other view initializing work
.
*/
if(ds == null){
//execute asynctask and set data to ds in asynctask
} else {
// set data to view.
}
return view;
}
}

Navigation drawer with activities instead of fragments

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);

can I use ListFragment with Navigation Drawer?

I am working on an android app and I want to make a ListFragment and use it in navigation drawer as a fragment. It is possible?
I make the ListFragment but i have errors on my main activity:
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
ListFragment lf=null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
case 2:
fragment = new PhotosFragment();
break;
case 3:
lf = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
if (lf != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, lf).commit();
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
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");
}
The error is: the method replace(int,Fragment) in the type FragmentFransaction is not applicable for the arguments (int,ListFragment).
This error is at this line of code(at replace):
if (lf != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, lf).commit();
where lf is a ListFragment.
So can I use ListFragment with Navigation Drawer?
You are using ListFragment from android.support.v4.app.ListFragment. If you indeed want to use support package, your Activity should extend FragmentActivity and then you should call getSupportFragmentManager() instead of getFragmentManager(). Otherwise please use android.app.ListFragment. So, probaly you should replace this:
import android.support.v4.app.ListFragment;
with this:
import android.app.ListFragment;

How can I able to call FragmentActivity containing viewpager with three tabs from Navigation Drawer Item?

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.

Categories

Resources