简体   繁体   English

Android部分持久保存的InstanceState

[英]Android partially persistent savedInstanceState

I have an android fragment that only restores the last portion of a class object when the orientation changes. 我有一个android片段,仅当方向更改时才还原类对象的最后一部分。 My code is below: 我的代码如下:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments().containsKey(ARG_ITEM_ID)) {
        // Load the weapon content specified by the fragment
        mWeapon = EquipmentContent.WEAPON_MAP.get(getArguments().getString(ARG_ITEM_ID));
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.main_fragment, container, false);

    // get reference to the ImageViews
    weaponImageView = (ImageView) rootView.findViewById(R.id.picture);
    groupImageView = (ImageView) rootView.findViewById(R.id.groupSymbol);
    individualImageView = (ImageView) rootView.findViewById(R.id.individualSymbol);

    // get a reference to the linear layout
    statLinearLayout = (LinearLayout) rootView.findViewById(R.id.statLinearLayout);

    if (mWeapon != null) {

        AssetManager assets = getActivity().getAssets();
        InputStream pictureStream;
        InputStream groupStream;
        InputStream individualStream;

        try
        {
           // get an InputStream to the asset representing the next picture
            pictureStream = assets.open("Pictures/" + mWeapon.picture + ".png");

            // load the asset as a Drawable and display on the ImageView
            Drawable picture = Drawable.createFromStream(pictureStream, mWeapon.picture);
            weaponImageView.setImageDrawable(picture);
        }
        catch (IOException e)
        {
            Log.e("MainFragment", "Error loading " + mWeapon.picture + ".png", e);
        }
        try
        {
            // get an InputStream to the asset representing the next picture
            groupStream = assets.open("Group/" + mWeapon.group + ".png");

            // load the asset as a Drawable and display on the ImageView
            Drawable group = Drawable.createFromStream(groupStream, mWeapon.group);
            groupImageView.setImageDrawable(group);
        }
        catch (IOException e)
        {
            Log.e("MainFragment", "Error loading " + mWeapon.group + ".png", e);
        }
        try
        {
            // get an InputStream to the asset representing the next picture
            individualStream = assets.open("Individual/" + mWeapon.individual + ".png");

            // load the asset as a Drawable and display on the ImageView
            Drawable individual = Drawable.createFromStream(individualStream, mWeapon.individual);
            individualImageView.setImageDrawable(individual);
        }
        catch (IOException e)
        {
            Log.e("MainFragment", "Error loading " + mWeapon.individual + ".png", e);
        }
        ((TextView) rootView.findViewById(R.id.item_detail)).setText(mWeapon.id); // Sets the title text of the fragment
        populateStatistics(inflater);


    }
    return rootView;
}

public void populateStatistics(LayoutInflater inflater) {
    // TODO: currently reverts all EditText views to the last input upon orientation change
    if (!mWeapon.priWeapon.equals("null")) { // if the primary weapon is not null, then inflate relevant TextViews
        TableLayout priWeaponIDTableLayout = (TableLayout) inflater.inflate(R.layout.statistics_text_view, null); // Weapon title statistic
        statLinearLayout.addView(priWeaponIDTableLayout);
        ((TextView) priWeaponIDTableLayout.findViewById(R.id.category)).setText(R.string.priWeapon);
        ((EditText) priWeaponIDTableLayout.findViewById(R.id.statistic)).setText(String.format("%s", mWeapon.priWeapon));
        TableLayout priWeaponRangeTableLayout = (TableLayout) inflater.inflate(R.layout.statistics_text_view, null); // Weapon range statistic
        statLinearLayout.addView(priWeaponRangeTableLayout);
        ((TextView) priWeaponRangeTableLayout.findViewById(R.id.category)).setText(R.string.priMaxRange);
        ((TextView) priWeaponRangeTableLayout.findViewById(R.id.category)).setTypeface(null, Typeface.NORMAL);
        ((TextView) priWeaponRangeTableLayout.findViewById(R.id.statistic)).setTypeface(null, Typeface.NORMAL);
        ((EditText) priWeaponRangeTableLayout.findViewById(R.id.statistic)).setText(String.format("%s meters", mWeapon.priMaximumRange));
        TableLayout priWeaponPenTableLayout = (TableLayout) inflater.inflate(R.layout.statistics_text_view, null); // Weapon penetration statistic
        statLinearLayout.addView(priWeaponPenTableLayout);
        ((TextView) priWeaponPenTableLayout.findViewById(R.id.category)).setText(R.string.priPen);
        ((TextView) priWeaponPenTableLayout.findViewById(R.id.category)).setTypeface(null, Typeface.NORMAL);
        ((TextView) priWeaponPenTableLayout.findViewById(R.id.statistic)).setTypeface(null, Typeface.NORMAL);
        ((EditText) priWeaponPenTableLayout.findViewById(R.id.statistic)).setText(String.format("%s mm", mWeapon.priPenetration));
    }

} }

The custom layout that I inflate and modify is below. 我在下面进行充气和修改的自定义布局。

 <?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tableLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:stretchColumns="*">
    <!-- Table Row 0 -->
    <TableRow
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/tableRow0">
        <TextView
            android:id="@+id/category"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/priWeapon" android:textColor="#000" android:textStyle="bold"
            android:layout_weight="1"
            android:gravity="left"
            android:padding="5dp"/>
        <EditText
            android:id="@+id/statistic"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/NA" android:textSize="14sp" android:textStyle="bold"
            android:gravity="center"
            android:focusable="false"
            android:layout_weight="1"
            android:cursorVisible="false"
            android:longClickable="false"/>
    </TableRow>
</TableLayout>

Almost everything loads fine when the orientation changes, to include the title, the picture .png, the group and individual .pngs, the weapon id, and the weapon penetration statistic. 方向更改时,几乎所有内容都可以正常加载,包括标题,图片.png,组和单个.png,武器ID和武器穿透统计信息。 The problem is that the weapon penetration statistic replaces the primary weapon id and maximum range for some reason. 问题在于,由于某种原因,武器穿透统计信息会替换主要武器ID和最大射程。 I'm not exactly sure why. 我不确定为什么。 Any assistance is appreciated. 任何帮助表示赞赏。

I tinkered with my code and changed the common flawed denominator view type, 'EditText', to 'TextView'. 我修改了代码,并将常见的有缺陷的分母视图类型“ EditText”更改为“ TextView”。 It now retains the data that it's supposed to. 现在,它保留了应有的数据。 For some reason EditText savedInstanceState isn't persistent but TextView is. 由于某种原因,EditText savedInstanceState不是持久性的,而TextView是持久性的。 I was using EditText originally because the Dietel book I was using used it for the benefit of the underline it provided. 我最初使用EditText的原因是因为我使用的Dietel书将其用于提供下划线的好处。 Now I'll just have to create a custom background to replicate that underline and apply it in the XML. 现在,我只需要创建一个自定义背景来复制下划线并将其应用到XML中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM