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 PhotosFragment();
break;
case 2:
fragment = new FindPeopleFragment();
break;
case 3:
fragment = new CommunityFragment();
break;
case 4:
fragment = new PagesFragment();
break;
case 5:
fragment = new WhatsHotFragment();
break;
default:
break;
}
its a fragment object calling different constructor of fragment how can i use constructor or any activity here in place of fragment constructor
Assuming that the context of this piece of code is within an Activity or Fragment simply call startActivity(new Intent(context, HomeActivity.class)). Obviously you have to have the respective Activities for the current fragments.
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.
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;
}
The navigation drawer lags every time I open a new activity. I looked on Google for a solution and I found out that I can solve it by delaying the new activity with a Handler. I experimented a little bit but got nowhere.
Some code pieces from MainActivity.java:
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 2:
fragment = new FragmentZero();
break;
case 3:
fragment = new FragmentOne();
break;
case 4:
fragment = new FragmentTwo();
break;
case 5:
fragment = new FragmentThree();
break;
case 7:
fragment = new FragmentTwo();
break;
case 8:
fragment = new FragmentZero();
break;
case 9:
fragment = new FragmentOne();
break;
case 10:
fragment = new FragmentTwo();
break;
case 11:
fragment = new FragmentZero();
break;
case 12:
fragment = new FragmentOne();
break;
case 14:
fragment = new FragmentZero();
break;
case 15:
fragment = new FragmentOne();
break;
case 16:
fragment = new FragmentTwo();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
and
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
if (dataList.get(position).getTitle() == null) {
SelectItem(position);
}
}
}
In your activity create a handler and initialize it in your on create method
private Handler mHandler;
mHandler = new Handler();
then change your drawer item click listener to this.
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position,
long id) {
if (dataList.get(position).getTitle() == null) {
mHandler.postDelayed(new Runnable() {
#Override
public void run() {
SelectItem(position);;
}
}, 250);
}
mDrawerList.setItemChecked(possition, true);
mDrawerLayout.closeDrawer(mDrawerList);
}
}
and change SelectItem method to this
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 2:
fragment = new FragmentZero();
break;
case 3:
fragment = new FragmentOne();
break;
case 4:
fragment = new FragmentTwo();
break;
case 5:
fragment = new FragmentThree();
break;
case 7:
fragment = new FragmentTwo();
break;
case 8:
fragment = new FragmentZero();
break;
case 9:
fragment = new FragmentOne();
break;
case 10:
fragment = new FragmentTwo();
break;
case 11:
fragment = new FragmentZero();
break;
case 12:
fragment = new FragmentOne();
break;
case 14:
fragment = new FragmentZero();
break;
case 15:
fragment = new FragmentOne();
break;
case 16:
fragment = new FragmentTwo();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
setTitle(dataList.get(possition).getItemName());
}
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;
}
}
I'm using ViewPager in a fragment. It has 3 tabs
Here's the code of my fragmentOagerAdapter:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
#Override
public CharSequence getPageTitle(final int position) {
String res = "";
switch (position) {
case 0:
res = "nameA";
break;
case 1:
res = "nameB";
break;
case 2:
res = "nameC";
break;
}
return res;
}
#Override
public int getCount() {
return 3;
}
I'm adding a a button in my main fragment that will allow me to switch between a list display and a grid display.
So what I want is to have a dynamic fragment depending on some enum stored within the Preferences
I want to do this for the 1st tab (position 0)
So I do the following:
#Override
public Fragment getItem(final int position) {
Fragment fragment = null;
switch (position) {
case 0:
switch (Settings.getInstance().getProductDisplayMode()) {
case LARGE_GRID:
fragment = Fragment.instantiate(this.context, ClassALargeGrid.class.getName());
break;
case SMALL_GRID:
fragment = Fragment.instantiate(this.context, ClassASmallGrid.class.getName());
break;
case LIST:
default:
fragment = Fragment.instantiate(this.context, ClassA.class.getName());
break;
}
break;
case 1:
fragment = Fragment.instantiate(this.context, ClassB.class.getName());
break;
case 2:
fragment = Fragment.instantiate(this.context, ClassC.class.getName());
break;
}
return fragment;
}
...
So it works fine when first opening the activity, but I don't know how to switch the fragment displayed in the first tab dynamically.
I tried calling destroyItem() on the FragmentPagerAdapter but it doesn't help...
Any idea ?
I actually had this problem a while back. I wound up having to take the fragment out of the viewpager and then use the FragmentManager to swap out the fragments dynamically.
Then, when I wanted to switch to another "page," I wrote some custom animations to "slide in" a fragment that hosted a ViewPager
Not a pretty solution, but it worked.