• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Trying to update fragment from another fragment

Deemar

Newbie
Feb 7, 2012
38
2
Hi, I have an activity which defines 5 fragments in a ViewPager and I can swipe between all 5 fragments.

Java:
    private void setupViewPager(ViewPager viewPager)
    {
        adapter = new ViewPagerAdapter(getSupportFragmentManager());
        quoteDetailsFragment =new QuoteDetailsFragment();
        quoteViewFragment =new QuoteViewFragment();
        quoteModifiersFragment =new QuoteModifiersFragment();
        quoteViewFragment2 = new QuoteViewFragment2();
        quoteContractFragment = new QuoteContractFragment();
        adapter.addFragment(quoteDetailsFragment,"Details");
        adapter.addFragment(quoteViewFragment,"CHAT");
        adapter.addFragment(quoteModifiersFragment,"CONTACTS");
        adapter.addFragment(quoteViewFragment2, "TAB44");
        adapter.addFragment(quoteContractFragment, "TAB55");
        viewPager.setAdapter(adapter);
    }

I defined a public method which quoteDetailsFragment can call in order to update the view in quoteViewFragment.

Java:
    public void updateQuoteView()
    {
        quoteViewFragment.loadQuoteValues();
    }

In quoteViewFragment, it reloads all of the fields to reflect the changes in the quoteDetailsFragment which is where all the checkboxes and edittexts are located. That's where you modify inputs for the quote.

Java:
    public void loadQuoteValues()
    {
        Double quotePriceValue = quote.calculateQuotePrice();

        // remove all views before populating the view again
        optionsTable.removeAllViews();
        buildingDetails.setText(String.valueOf(quote.getBuildingWidth()) + "' X " + String.valueOf(quote.getBuildingLength() + "' X " + String.valueOf(quote.getBuildingHeight() + "' " + quote.getBuildingType())));
        quotePrice.setText(String.valueOf(defaultFormat.format(quotePriceValue)));
        quotePrice2.setText(String.valueOf(defaultFormat.format(quotePriceValue)));

..................

Okay, here's the problem. If I remove everything from loadQuoteValues() and have it only update one single text field with a static string of characters, it takes about 1 second, which is the same amount of time it takes to update the entire quote, so there's no way to make the update take less time. Therefore, if I need to spend this second to update, I need to decide when I'm going to process the update on the quoteViewFragment side.

I tried the update in an asynchronous task but that doesn't allow me to update the view elements from a thread outside the user interface.

I tried the update when the user swipes but that was causing the swipe to hang for a second and skip a bunch of frames while it processed.

I tried running it inside an onTextChangedListener on each editText but then the editText inputs would hang for a second after each character input while the process ran.

I tried running it on editText blur through a focusChangeListener which worked great except the user will sometimes edit an editText and then swipe to the quote while the editText still has focus. Then the quote isn't being update with this last editText change unless the user first clicks into a different editText and then swipes.

Java:
        for (final EditText editText : editTexts) { //need to be final for custom behaviors
            editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                @Override
                public void onFocusChange(View v, boolean hasFocus) {
                    if (!hasFocus)
                    {
                        // for blank inputs, like deleting the width to enter a new value
                        String editValue = editText.getText().toString().equals("") ? "0" : editText.getText().toString();

                        if(editText == buildingWidth){
                            quote.setBuildingWidth(Integer.parseInt(editValue));
                        }
                        else if(editText == buildingLength){
                            quote.setBuildingLength(Integer.parseInt(editValue));
                        }

................................

As you can see, the editText listener updates the quote model and when the quoteViewFragment updates, it gets all data from the same quote model and recreates itself based on these values.

How can I update the second fragment without having the swipe nor input hang while it processes? My main goal here is to give users a screen with all inputs which they can modify based on what the customer wants and then they can swipe to see the quote already built, based on their inputs. It is acceptable to have a spinning wheel after swipe while the quote loads but I can't figure out how to get a callback after the swipe is complete. It always runs as soon as I lift my finger.
 
I assume this is related to your other thread, which we didn't really get a satisfactory resolution to

https://androidforums.com/threads/l...ess-items-from-database.1286888/#post-7801332

Because you didn't resolve this problem, it's caused other knock on problems.
Clearly, having the user wait for a second when swiping between tabs is not acceptable, so you have a few options:-

1. Find the cause of the delay and resolve it.
2. Don't do the slow operation on swiping of tabs
3. Have some other trigger for doing the update e.g. place a button on the screen

But the bottom line is, you need to work out what's causing the massive delay in calculating your quotes. It really shouldn't take that long.
 
Upvote 0
This isn't in calculating the quotes, it's the same program but an unrelated problem.
I commented out everything to do with calculating the quote and simply added another line:
Java:
editText1.setText("aaa");
Commenting that line in and out will add / remove a second to the operation. That's why I figured there's no way to speed it up because it's literally just writing a static string into an editText.
I was able to resolve this by checking the positionOffsetPixels in the pageChangeListener for the viewPager. The onPageScrolled event runs repeatedly while the scrolling is happening and the positionOffsetPixels only equals 0 when the page is completely scrolled. I wait for this before running the 1 second update and everything is great, the page scrolls without hanging.

Java:
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                // only update after the page swipe has completed
                if (position == 1 && positionOffsetPixels == 0 && quote.getEditable())
                    updateQuoteView();
            }

            @Override
            public void onPageSelected(int position) {
                tabLayout.getTabAt(position).select();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones