ITKeyword,专注技术干货聚合推荐

注册 | 登录

Erratic behaviour of Android customised buttons within fragments controlled by one activity, where they exhibit a repeating pattern

itPublisher 分享于

2021腾讯云限时秒杀,爆款1核2G云服务器298元/3年!(领取2860元代金券),
地址https://cloud.tencent.com/act/cps/redirect?redirect=1062

2021阿里云最低价产品入口+领取代金券(老用户3折起),
入口地址https://www.aliyun.com/minisite/goods

I have a repeating pattern of behaviour (or mis behaviour!) by my customised buttons, when the user navigates from a choice fragment, (deciding between new and existing records) where the choice taken is for Existing records, which leads to a fragment with a populated spinner with a drop down list of existing records, with a retrieve button. The action being to select the record from the drop down list and then click the retrieve button.

Fig 1 (patientNotesFg)

Fig 2 (findPatientRecordFg)

Fig 3 record detail display

The retrieve button click, should then lead to the record details fragment, which is a whole selection of edit text fields which display the existing record, and significantly have a number of further customised buttons at the bottom of the fragment (back, update, analysis, etc). The back button is currently the only one with code behind it which takes the user back to the New/Existing record choice fragment. So we have a cycle of ,

  1. chose existing record, (Fig 1)

  2. chose from spinner list, (Fig 2)

  3. click retrieve button, (Fig 2)
  4. see record in display fragment (Fig 3), and
  5. click back button, to return to New/Existing record choice fragment. (Fig 1)

For simplicity I have been using records 1,2 & 3, entitled Record 1, Record 2, Record 3, to select and try and display in the detail display fragment (4. above) sequentially. I think I've established that its not the record content which causes the 'misbehaviour' more the code behaviour around the record handling which I cannot de-bug.

The erratic button behaviour is the number of times the button has to be clicked before it does what its supposed to do, and with in the cycle, what the result is.

On starting we get to fig 1, chose Existing and get to fig 2 the spinner. Chose Record 1 & hit retrieve. This displays record 1, which is fine. Now we hit back button, this requires 2 presses/clicks before it actions and takes you back to fig 1. Chose Existing records, and in the spinner, chose Record 2, click retrieve, and record 2 is displayed, which is fine. Hit back button, 1 click takes you back to the 'top' fragment - Fig 1 .

Now chose Existing records again, and at the spinner chose Record 3 and click retrieve. Now this requires 2 clicks to action, but here's the next problem, the record displayed is Record 2. Now hit back button, requires only 1 click and it takes you back to the Fig 1 top fragment. Again chose existing, chose Record 3, click retrieve, 1 click only required, and Record 3 is displayed. Click back button, single click required, & you're back at the top fragment as Fig 1.

To summarise this cycle I've created a small spreadsheet of the results to show the pattern that’s emerged !!

All the buttons have an OnClick line in their layout xml file, which leads back to an associated method in the main activity 'MainActivity'.

The patient details back button's layout xml is as follows :-

<com.example.martin.ph_program_test3.ButtonPlus
    android:id="@+id/Patient_Details_Back"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    style="@style/CustomButton"
    android:text="@string/Back"
    android:layout_marginTop="20dp"
    android:layout_below="@id/Notes"
    android:layout_alignStart="@id/Notes"
    android:onClick="patient_Details_Back"
    />

And the ButtonPlus java file is as follows :-

package com.example.martin.ph_program_test3;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.Button;

/**
 * Created   on 09/09/2015.
 */
public class ButtonPlus extends Button {
public ButtonPlus(Context context) {
    super(context);
}

public ButtonPlus(Context context, AttributeSet attrs) {
    super(context, attrs);
 }

public ButtonPlus(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
 }


   public void setTypeface(Typeface tf, int style){
     if(style == Typeface.BOLD){
         super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "TahomaBold.ttf") );
     }else
     if (style == Typeface.NORMAL){

       super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Plain Germanica.ttf"));
     }else
     if (style == Typeface.ITALIC){
         super.setTypeface(Typeface.createFromAsset(getContext().getAssets(), "Tahoma Faux Italic.ttf"));  
     }
    }
}

The OnClick method within MainActivity which deals with the 'Back' button click from the detailed record display 'patient_Details_Back' is as follows :-

public void patient_Details_Back(View view) {

Log.d(DEBUGTAG, " ---->>>>>>>  inside patient details back  L 345  ");

patientID = 0;

    if (patientNotesFg != null) {
        Log.d(DEBUGTAG, " ---->>>>>>>  patientNotesFg  NOT null L 391  ");

        trans = getFragmentManager().beginTransaction();
        trans.replace(R.id.fragment_container, patientNotesFg);
        trans.addToBackStack(null);
        Log.d(DEBUGTAG, " ---->>>>>>>  patientNotesFg  NOT null L 396  ");
        trans.commit();
        Log.d(DEBUGTAG, " ---->>>>>>>  patientNotesFg  NOT null L 398  ");
    } else if (patientNotesFg == null) {
        Log.d(DEBUGTAG, " ---->>>>>>>  patientNotesFg  ==  null  L 400  ");
        patientNotesFg = new PatientNotesFg();
        trans = getFragmentManager().beginTransaction();
        trans.add(R.id.fragment_container, patientNotesFg);
        trans.addToBackStack(null);
        trans.commit();
}        
}

I have placed a whole bunch of displays to track the execution, and where this button requires 2 clicks to work, displays on lines 391-398 occur twice, but only action the code on the second run through!

On a similar vein where the retrieve button requires 2 clicks to work, I have similarly placed displays in the code to track the execution and I get the same double display of the debug tags as per lines 716, 727, 729 but nothing happens on the first run through, only after the second click do I get the display of the record.

The code for the retrieve onClick method is as follows :-

public void retrievePatientRecord(View view) {

        Toast.makeText(this,"  ******   INSIDE RETRIEVE PATIENT RECORD  - BUTTON PRESS   *****  ", Toast.LENGTH_LONG).show();
//   the patient has been chosen, now get the patient-ID from  findPatientRecordFg
        this.patientID = findPatientRecordFg.patientID;

//  clear down the current values in this class, & the controller class            
    clearDownCurrentStringValues();
    runClearDownControllerSetters();


   Log.d(DEBUGTAG, " ---->>>>> retrieve this record L 664 with ID =  " + findPatientRecordFg.patientID);

// call to controller for (DB query for) existing patient's records
        controller.retrievePatientRecords(patientID);

// populate patientDetails fragment fields and re-display patient details fragment.

      if (existingPatientFg != null) {
      Log.d(DEBUGTAG, " ---->>>>> NOT NULL existingPatientFg  @  L 716   ID =  " + existingPatientFg);
            populatePatientDetails_fgWithCurrentValues();
// clear down the bundle's existing arguments
            existingPatientFg.getArguments().clear();
// refresh the bundle with the new arguments
            addStringsToArguments(bundle);

// do the fragment replace, & display
            trans = getFragmentManager().beginTransaction();
            trans.replace(R.id.fragment_container, existingPatientFg);
            trans.addToBackStack(null);
            Log.d(DEBUGTAG, " ---->>>>> NOT NULL existingPatientFg  @  L 727 ");
            trans.commit();
            Log.d(DEBUGTAG, " ---->>>>> NOT NULL existingPatientFg  @  L 729 ");
        }else
        if (existingPatientFg == null){

            existingPatientFg = new ExistingPatientFg();
            Log.d(DEBUGTAG, " ---->>>>> existingPatientFg  @  L 734   ID =  " + existingPatientFg);
            populatePatientDetails_fgWithCurrentValues();
            bundle = new Bundle();
            addStringsToArguments(bundle);
            existingPatientFg.setArguments(bundle);
            trans = getFragmentManager().beginTransaction();
            trans.add(R.id.fragment_container, existingPatientFg);
            trans.addToBackStack(null);
            trans.commit();


        }
    }

As to what is happening where the retrieve button requires 2 clicks and then displays the last record not the one being requested, that’s a mystery to me at the moment, I'm looking into see if that’s a DB query problem, but since it only happens with the double click event, I'm not so sure it’s a DB issue!

I've seen one S/O question where the particular parent theme was implicated in some buttons behaviour (I haven't been able to find this one again so can't post the link) , but I do remember it was a different theme to the one I'm using, here's my style.xml file :-

<resources>

    <!-- Base application theme. -->
    <style
        name="AppTheme"
        parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
    </style>

    <style
        name="button" parent="@android:style/Widget.Button">
    </style>

    <style
        name="CustomButton" parent="@android:style/Widget.Button">
        <item name="android:textAllCaps">false</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#0c0c0c</item>
        <item name="android:includeFontPadding">false</item>
        <item name="android:padding">5dp</item>
        <item name="android:minHeight">15dip</item>
        <item name="android:maxHeight">35dip</item>
        <item name="android:minWidth">105dip</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:focusable">true</item>
        <item name="android:clickable">true</item>
        <item name="android:background">@drawable/states</item>
    </style>

    <style
        name="CustomButtonGrey" parent="@android:style/Widget.Button">
        <item name="android:textAllCaps">false</item>
        <item name="android:layout_width">fill_parent</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:textStyle">bold</item>
        <item name="android:textSize">12sp</item>
        <item name="android:textColor">#939393</item>
        <item name="android:includeFontPadding">false</item>
        <item name="android:padding">5dp</item>
        <item name="android:minHeight">15dip</item>
        <item name="android:maxHeight">35dip</item>
        <item name="android:minWidth">105dip</item>
        <item name="android:layout_gravity">center_horizontal</item>
        <item name="android:background">@drawable/states</item>
    </style>

</resources>

So after this rather large exposition of information I'm hoping this may be sufficient to allow someone more knowledgeable in the ways of android to be able to help me solve this problem, which has really got me beat at the moment! Thanks in advance !

android android-fragments button android-activity
asked Feb 13 '16 at 20:50 Martin Clifford 19 6      Thanks in advance ! ? No. No. We want explicit thank after you have been helped. Dont run away like that. –  greenapps Feb 14 '16 at 11:09      I have no intention of 'running away' like anything...., I'm indicating my gratitude of anyone taking the time to read through my problem in order to be able to help me! –  Martin Clifford Feb 15 '16 at 12:09      FYI, I've tested this logic with bog standard buttons and get the exact same result, so its very probably not a button issue, more likely the inter-relationship between Activities, fragments and how the onClick() method works. I am still stuck, needing help though! –  Martin Clifford Feb 21 '16 at 16:09      This is much too much ĉode to deep in. Sorry but if you cannor post much less code that misbehaves the same way think that nobody will take the effort to read that all. Cannot you make an example app with say one activity and two fragments which demonstrates your issue? –  greenapps Feb 21 '16 at 16:51      It is unclear what we see in Fig 1, 2 and three. Activity? A fragment? Can you add it to the description in the post? Why is fig 3 so much bigger? –  greenapps Feb 21 '16 at 16:58  |  show more comments

1 Answers
1

In order to try and find a solution to this problem, I wrote a program going back to the very basics, which emulated the essential navigation logic of this program. I re-created this double button click problem, but noticed it also involved the rather peculiar display of the patient details fragment as shown in Fig 3 when the double click happened. It occurred to me that maybe the fragment_container was getting confused about what it was trying to display because there was more than one existingPatientFg (which inflates the patient details fragment - as in fig 3) within it at certain times. So I inserted this next piece of code where ever the existingPatientFg was being dispensed with by for example, a navigation button click to another fragment.

if (existingPatientFg != null){
    trans = getFragmentManager().beginTransaction();
    trans.remove( existingPatientFg);
    trans.commit();
}

and this solved the problem and the double button click disappeared.

My explanation of the problem may be wrong, even though my solution works & after quite some testing, just to make sure.

My additional conclusion is that you also have to be quite careful about which command you use to effect the different display of a fragment as in …

trans.replace(R.id.fragment_container, patientNotesFg);

or

trans.add(R.id.fragment_container, patientNotesFg);

while they seemingly give the same result, 'adding' to the container appears to possibly create a cumulative type problem, which may require an active 'clearing out' approach ie some removals, to avoid the kind of problem I stumbled on and which had defeated me for some months!


answered Apr 20 '16 at 19:44 Martin Clifford 19 6

 | 


相关阅读排行


相关内容推荐

最新文章

×

×

请激活账号

为了能正常使用评论、编辑功能及以后陆续为用户提供的其他产品,请激活账号。

您的注册邮箱: 修改

重新发送激活邮件 进入我的邮箱

如果您没有收到激活邮件,请注意检查垃圾箱。