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;
}
}
Related
I need to replace fragment in view pager in an activity on a nework call success in another fragment. View pager is inside my dashboard activity. Here is get item method
#Override
public Fragment getItem(int pos) {
Fragment fragment = null;
switch (pos) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new SponsorshipOffersFragment();
break;
case 2:
if (mPreferenceManager.getConnectProfileStatus()) {
fragment = new CollaborationFragment();
} else {
fragment = new ConnectSettingFragment();
}
break;
case 3:
fragment = new ProtfolioFragment();
break;
case 4:
fragment = new SocialSummaryFragment();
break;
}
return fragment;
}
For case 2, first time getConnectProfileStatus will return false and ConnectSettingFragment will get call. After submitting profile from ConnectSettingFragment, on success I need to call CollaborationFragment.
Can someone help me to do ? I am calling below method from fragment
public void replaceFrament() {
mViewPager.setCurrentItem(2);
}
that is inside activity but nothing happening.
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;
}
I'm using the native Android drawer for my menu, here in MainActivity :
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
//Home
fragment = new EntryFragment();
break;
case 1:
//Journal
fragment = new JournalFragment();
break;
case 2:
// Medications
fragment = new RxFragment();
break;
case 3:
// Reminders
fragment = new ReminderFragment();
break;
case 4:
// Counselor Locator
fragment = new LocatorFragment();
break;
case 5:
// Library
fragment = new LibraryFragment();
break;
case 6:
// Settings
break;
case 7:
// About
break;
default:
break;
}
if (fragment != null) {
fm.beginTransaction()
.replace(R.id.container, fragment).addToBackStack(String.valueOf(position)).commit();
String fragmentTitles[] = getResources().getStringArray(R.array.menu_array);
if(mActionBar != null) {
mActionBar.setTitle(fragmentTitles[position]);
}
} else {
Log.e("MainActivity", "Error in creating fragment");
}
But I'm repurposing EntryFragment for another activity (EntryDetailActivity), so in EntryFragment I defined a couple of methods to modify the view based on which activity is hosting it:
// Runs on the home page, shows/hides appropriate views
public void prepareForHomeView() {
mTxtDate.setVisibility(View.GONE);
mBtnDiffDay.setVisibility(View.GONE);
mEdtNotes.setVisibility(View.GONE);
mBtnSave.setVisibility(View.GONE);
mBtnDelete.setVisibility(View.GONE);
}
// Runs on the detail page, shows/hides appropriate views
public void prepareForDetailView() {
mBtnLog.setVisibility(View.GONE);
}
In EntryDetailActivity, I can easily call prepareForDetailView() in onResumeFragments(), but I'm having an issue of where to call prepareForHome() in MainActivity, since I am swapping fragments in and out based on the menu. Any suggestions?
/** Update **/
I assigned the fragments a tag that is the position of the menu through addToBackstack(). I also added the following method to my MainActivity:
#Override
public void onAttachFragment(Fragment fragment) {
super.onAttachFragment(fragment);
FragmentManager fm = getSupportFragmentManager();
EntryFragment entryFrag = (EntryFragment)fm.findFragmentByTag("0");
if(entryFrag != null) {
Log.v("rx", "Home frag is not null");
} else {
Log.v("rx", "Home frag is null");
}
}
Activity (or FragmentActivity) has onAttachFragment(Fragment fragment) - you could do an instanceof on fragment to determine if it is the correct type of fragment (or use the id, tag, etc) and call the appropriate method.
Alternatively, you could have the Fragment check the getActivity() (in onCreateView / onViewCreated).
/**
* 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.