I am displaying DialogFragment from a manager. DialogFragment display multiple times.
I want to know is there a way to check from transaction whether this fragment already displaying. So don't display it.
#Override
public void show(FragmentManager manager, String tag) {
try {
FragmentTransaction ft = manager.beginTransaction();
Fragment prev = manager.findFragmentByTag(tag);
if (prev == null) {
ft.add(this, tag);
///ft.addToBackStack(tag);
ft.commitAllowingStateLoss();
}
} catch (IllegalStateException e) {
Log.d("ABSDIALOGFRAG", "Exception", e);
}
}
I am calling my Fragment like
CustomerFeedbackDialog feedbackDialog = CustomerFeedbackDialog.newInstance(genaric.getData(), type);
feedbackDialog.show(getSupportFragmentManager(), "feedbackDialog");
I have call findFragmentByTag but it is always null. I don't want to show already displayed Fragment. otherwise it duplicate . Mulitple dialogFragment opens
I know I can do it using a flag in sharedprefs
EDIT Solution found
Thanks for your help. Problem solved and I posted answer below
If your problem is the same DialogFragment over another you can try adding a variable to your Manager:
#Nullable
private DialogFragment mCurrentDialogFrag;
And then whenever you add a new DialogFragment you set mCurrentDialogFrag to the new DialogFragment and then check before adding if current DialogFragment is the same as the new one.
I finally able to handle it by overriding show method
and addToBackStack(null) and executePendingTransactions
Firstly to put tag in findFragmentByTag addToBackStack is must otherwise tag is not added in fragmentTransaction.
#Override
public void show(FragmentManager manager, String tag) {
try {
FragmentTransaction ft = manager.beginTransaction();
Fragment prev = manager.findFragmentByTag(tag);
if (prev == null) {
ft.add(this, tag);
ft.addToBackStack(null);
ft.commitAllowingStateLoss();
manager.executePendingTransactions();
}
} catch (IllegalStateException e) {
Log.d("ABSDIALOGFRAG", "Exception", e);
}
}
Now if fragment is already in transaction. It will not display again..
If I look into DialogFragment's public void show(FragmentManager manager, String tag) method implementation, I can see this:
public void show(FragmentManager manager, String tag) {
this.mDismissed = false;
this.mShownByMe = true;
FragmentTransaction ft = manager.beginTransaction();
ft.add(this, tag);
ft.commit();
}
In order to know my dialog is already part of FragmentManager transactions, I'm simply doing this:
if (getFragmentManager().findFragmentByTag("...the tag") == null) {
fragment.show(getFragmentManager(), "...the tag");
}
I have to mention the above is strictly appcompat experience, and it works. Obviously you can put this check code inside override of show as well.
Related
I am using an open source project(https://github.com/chrisbanes/cheesesquare) to develop my own application.
what I want is:
1.In the Home Fragment, there is a tablayout below to the toobar,and when the recycelview scroll, the Toolbar can hide, but the FloatingActionButton always stay;
2. In the Message Frgment, the is no Tab and on FloatingActionButton, only a simple blank Fragment with a Toolbar.
I try to do this in MainActivity:
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
menuItem.setChecked(true);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment fragment = null;
int id = menuItem.getItemId();
if (id == R.id.nav_home) {
fragment = new HomeFragment();
} else if (id == R.id.nav_message) {
fragment = new MessageFragment();
} else if (id == R.id.nav_friends) {
fragment = new FriendsFragment();
} else if (id == R.id.nav_discussion) {
fragment = new DiscussionFragment();
}
ft.replace(R.id.viewpager, fragment);
ft.commit();
mDrawerLayout.closeDrawer(GravityCompat.START);
return true;
}
});
}
but this not work, because the tablayout stay both in HomeFragment and MessageFragment.
When I try to do change the tablayout to the layout xml of HomeFragment, I also meet some problem because the below code should write in MainActivity
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
I feel puzzled, what should I do to achieve my goals?
I try to solve the problem like this, implement the basic functions.
Please advise is there a way to update tableview and set simple text url to hyperlink in a table column?
Thanks a lot.
Thanks for the tip I tried and it worked! after that I also tried to add XML adapter in the JAXB and it also worked.
public class XMLHyperLinkAdapter extends XmlAdapter<String, Hyperlink> {
#Override
public Hyperlink unmarshal(String v) {
try{
Hyperlink link = new Hyperlink();
link.setText(v);
link.setOnAction((ActionEvent e) -> {
link.setVisited(false);
System.out.println("This link is clicked: " + link.getText());
});
return link;
}catch (Exception e)
{
e.printStackTrace();
return null;
}
}
#Override
public String marshal(Hyperlink v) throws Exception {
return v.getText();
}
}
I'm using viewpager to display pictures. I just need three fragments basically: previous image to preview, current display image and next image to preview. I would like to just display a preview of previous and next image, it will change to full image when user actually swipe to it. So I'm thinking of just using 3 fragment to achieve this. Code is below:
private class ImagePagerAdapter extends FragmentStatePagerAdapter implements ViewPager.OnPageChangeListener {
private ImageFragment mImageFragment;
private ImagePreviewFragment mPreviousPreviewFragment;
private ImagePreviewFragment mNextPreviewFragment;
public ImagePagerAdapter(FragmentManager fm, ImageFragment image, ImagePreviewFragment previous, ImagePreviewFragment next) {
super(fm);
mImageFragment = image;
mPreviousPreviewFragment = previous;
mNextPreviewFragment = next;
}
#Override
public Fragment getItem(int position) {
if (position == mPager.getCurrentItem()) {
mImageFragment.display(position);
return mImageFragment;
}
if (position < mPager.getCurrentItem()) {
mPreviousPreviewFragment.display(position - 1);
return mPreviousPreviewFragment;
}
mNextPreviewFragment.display(position + 1);
return mNextPreviewFragment;
}
#Override
public int getCount() {
return 100;
}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.d(TAG, "onPageScrolled");
}
#Override
public void onPageSelected(final int position) {
Log.d(TAG, "onPageSelected " + position);
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
notifyDataSetChanged();
}
}, 500);
}
#Override
public void onPageScrollStateChanged(int state) {
Log.d(TAG, "onPageScrollStateChanged " + state);
}
#Override
public int getItemPosition(Object item) {
return POSITION_NONE;
//return POSITION_UNCHANGED;
}
}
So basically, I pre-created three fragments to display previous/next preview and current image and return them for getItem(). I also notifydatasetchange() in onpageselected() to make all three position to update the fragment when user swipe to new page.
But the problem is that it will throw out
Fragment already added IllegalStateException
when the fragments are added a second time. I think it's because it's been added before. I can create a new fragment every time but I think that's wasteful. So how can I reuse the already created fragment and just update them?
Thanks,
Simon
FragmentStatePagerAdapter design suggests creating a new Fragment for every page (see Google's example). And unfortunately you cannot readd a Fragment once it was added to a FragmentManager (what implicitly happens inside adapter), hence the exception you got. So the official Google-way is to create new fragments and let them be destroyed and recreated by the adapter.
But if you want to reuse pages and utilize an analogue of ViewHolder pattern, you should stick to views instead of fragments. Views could be removed from their parent and reused, unlike fragments. Extend PagerAdapter and implement instantiateItem() like this:
#Override
public Object instantiateItem(ViewGroup container, final int position) {
//determine the view type by position
View view = viewPager.findViewWithTag("your_view_type");
if (view == null) {
Context context = container.getContext();
view = LayoutInflater.from(context).inflate(R.layout.page, null);
view.setTag("your_view_type");
} else {
ViewGroup parent = (ViewGroup) item.getParent();
if (parent != null) {
parent.removeView(item);
}
}
processYourView(position, view);
container.addView(view, MATCH);
return view;
}
You should add some extra logic to determine the view type by position (since you have 3 types of views), I think you can figure that out.
I have a TreeView existing out of User objects. The TreeView represents the hierarchy of the Users:
Master1
Super1
Super2
User1
User2
Super3
User3
Super4
Master2
...
Every TreeItem is Collapsed when the TreeView is initialized. However, it can be that when the FXML is loaded, a TreeItem object is passed through from another FXML file. Eg: User3 has been passed through:
selectedUserTreeItem = (TreeItem<User>) currentNavigation.getPassthroughObject(); //this is the User3 TreeItem
I try to use a recursive function to expand all the parent nodes from the selecterUserTreeItem
if (selectedUserTreeItem != null) {
expandTreeView(selectedUserTreeItem);
}
tvUsers.setRoot(rootItem);
This is what I have so far:
private void expandTreeView(TreeItem<User> selectedItem) {
if (selectedItem != null) {
System.out.println(selectedItem);
if (selectedItem.isLeaf() == false) {
selectedItem.setExpanded(true);
}
TreeItem<User> parent = selectedItem.getParent();
expandTreeView(parent);
} else {
System.out.println("null");
}
}
I think it has to do something with the fact that the function is a void function and it should be returning a TreeItem object I suppose but for some reason I don't succeed in doing it.
Could someone point me in the right direction?
Ok, I notice that in your expandTreeView() method you expand the node and then you recurse to the previous node to expand that. In my own code I expanded from the root to the leaf, so lets try that:
private static <T> void expandTreeView(TreeItem<T> selectedItem) {
if (selectedItem != null) {
expandTreeView(selectedItem.getParent());
if (!selectedItem.isLeaf()) {
selectedItem.setExpanded(true);
}
}
}
No I don't think its because your method is returning void.
Try setting your TreeView root first before expanding:
>>> tvUsers.setRoot(rootItem);
if (selectedUserTreeItem != null) {
expandTreeView(selectedUserTreeItem);
}
If that doesn't work then try wrapping your initial expandTreeView() to run later:
Platform.runlater( new Runnable()
{
public void run() { expandTreeView( selectedUserTreeItem ); }
};
Ok, I had the same problem and found that I had to delay the expansion a bit like this:
Platform.runlater( new Task<Void>()
{
#Override
protected Void call() throws Exception
{
Thread.sleep( 250 );
return null;
}
#Override
protected void succeeded()
{
expandTreeView( selectedUserTreeItem );
}
} );
You may have to try and vary the delay to get it to work in your case.
From the examples at Xamarin.com you can build basic M.T. Dialog apps, but how do you build a real life application?
Do you:
1) Create a single DialogViewController and tree every view/RootElement from there or,
2) Create a DialogViewController for every view and use the UINavigationController and push it on as needed?
Depending on your answer, the better response is how? I've built the example task app, so I understand adding elements to a table, click it to go to the 'next' view for editing, but how to click for non-editing? How to click a button, go next view if answer is number 1?
Revised:
There is probably no one right answer, but what I've come up with seems to work for us. Number 2 from above is what was chosen, below is an example of the code as it currently exists. What we did was create a navigation controller in AppDelegate and give access to it throughout the whole application like this:
public partial class AppDelegate : UIApplicationDelegate
{
public UIWindow window { get; private set; }
//< There's a Window property/field which we chose not to bother with
public static AppDelegate Current { get; private set; }
public UINavigationController NavController { get; private set; }
public override bool FinishedLaunching (UIApplication app, NSDictionary options)
{
Current = this;
window = new UIWindow (UIScreen.MainScreen.Bounds);
NavController = new UINavigationController();
// See About Controller below
DialogViewController about = new AboutController();
NavController.PushViewController(about, true);
window.RootViewController = NavController;
window.MakeKeyAndVisible ();
return true;
}
}
Then every Dialog has a structure like this:
public class AboutController : DialogViewController
{
public delegate void D(AboutController dvc);
public event D ViewLoaded = delegate { };
static About about;
public AboutController()
: base(about = new About())
{
Autorotate = true;
about.SetDialogViewController(this);
}
public override void LoadView()
{
base.LoadView();
ViewLoaded(this);
}
}
public class About : RootElement
{
static AboutModel about = AboutVM.About;
public About()
: base(about.Title)
{
string[] message = about.Text.Split(...);
Add(new Section(){
new AboutMessage(message[0]),
new About_Image(about),
new AboutMessage(message[1]),
});
}
internal void SetDialogViewController(AboutController dvc)
{
var next = new UIBarButtonItem(UIBarButtonSystemItem.Play);
dvc.NavigationItem.RightBarButtonItem = next;
dvc.ViewLoaded += new AboutController.D(dvc_ViewLoaded);
next.Clicked += new System.EventHandler(next_Clicked);
}
void next_Clicked(object sender, System.EventArgs e)
{
// Load next controller
AppDelegate.Current.NavController.PushViewController(new IssuesController(), true);
}
void dvc_ViewLoaded(AboutController dvc)
{
// Swipe location: https://gist.github.com/2884348
dvc.View.Swipe(UISwipeGestureRecognizerDirection.Left).Event +=
delegate { next_Clicked(null, null); };
}
}
Create a sub-class of elements as needed:
public class About_Image : Element, IElementSizing
{
static NSString skey = new NSString("About_Image");
AboutModel about;
UIImage image;
public About_Image(AboutModel about)
: base(string.Empty)
{
this.about = about;
FileInfo imageFile = App.LibraryFile(about.Image ?? "filler.png");
if (imageFile.Exists)
{
float size = 240;
image = UIImage.FromFile(imageFile.FullName);
var resizer = new ImageResizer(image);
resizer.Resize(size, size);
image = resizer.ModifiedImage;
}
}
public override UITableViewCell GetCell(UITableView tv)
{
var cell = tv.DequeueReusableCell(skey);
if (cell == null)
{
cell = new UITableViewCell(UITableViewCellStyle.Default, skey)
{
SelectionStyle = UITableViewCellSelectionStyle.None,
Accessory = UITableViewCellAccessory.None,
};
}
if (null != image)
{
cell.ImageView.ContentMode = UIViewContentMode.Center;
cell.ImageView.Image = image;
}
return cell;
}
public float GetHeight(UITableView tableView, NSIndexPath indexPath)
{
float height = 100;
if (null != image)
height = image.Size.Height;
return height;
}
public override void Selected(DialogViewController dvc, UITableView tableView, NSIndexPath indexPath)
{
//base.Selected(dvc, tableView, path);
tableView.DeselectRow(indexPath, true);
}
}
#miquel
The current idea of a workflow is an app that starts with a jpg of the Default.png that fades into the first view, with a flow control button(s) that would move to the main app. This view, which I had working previous to M.T.D. (MonoTouch.Dialog), which is a table of text rows with an image. When each row is clicked, it moves to another view that has the row/text in more detail.
The app also supports in-app-purchasing, so if the client wishes to purchase more of the product, then switch to another view to transact the purchase(s). This part was the main reason for switching to M.T.D., as I thought M.T.D. would be perfect for it.
Lastly there would be a settings view to re-enable purchases, etc.
PS How does one know when the app is un-minimized? We would like to show the fade in image again.
I have been asking myself the same questions. I've used the Funq Dependency Injection framework and I create a new DialogViewController for each view. It's effectively the same approach I've used previously developing ASP.NET MVC applications and means I can keep the controller logic nicely separated. I subclass DialogViewController for each view which allows me to pass in to the controller any application data required for that particular controller. I'm not sure if this is the recommended approach but so far it's working for me.
I too have looked at the TweetStation application and I find it a useful reference but the associated documentation specifically says that it isn't trying to be an example of how to structure a MonoTouch application.
I use option 2 that you stated as well, it works pretty nicely as you're able to edit the toolbar options on a per-root-view basis and such.
Option 2 is more feasible, as it also gives you more control on each DialogViewController. It can also helps if you want to conditionally load the view.