2

我有一个包含两个片段的活动。该活动具有不同的纵向和横向布局,但片段保持不变(使用标签创建并重复使用)。在其中一个片段中,我有 xml 定义的 textview,它有 android:freezesText="true" 行,但在屏幕旋转后它仍然会丢失文本。为什么会发生这种情况?

片段的布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvDisplay"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/dip10"
        android:freezesText="true"
        android:gravity="center"
        android:text="@string/hello_world"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>

片段的类:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayFrag extends Fragment {

    @Override
    public View
            onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.display_frag, container, false);
        TextView tvDisplay = (TextView) view.findViewById(R.id.tvDisplay);
        tvDisplay.setText(getArguments().getString(Keys.NAME)
                + ", how are you? your phone number is " + getArguments().getString(Keys.PHONE));
        return view;
    }
}

活动:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragMan = getSupportFragmentManager();
        InputFrag inputFrag = (InputFrag) fragMan.findFragmentByTag(Keys.TAG_INPUT_FRAG);
        if (inputFrag == null) {
            fragMan.beginTransaction().add(R.id.hook1, new InputFrag(), Keys.TAG_INPUT_FRAG)
                    .commit();
        } else {
            fragMan.beginTransaction().replace(R.id.hook1, inputFrag, Keys.TAG_INPUT_FRAG).commit();
        }
    }

    public void submit(View view) {
        FragmentManager fragMan = getSupportFragmentManager();
        DisplayFrag displayFrag = (DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
        if (displayFrag == null) {
            displayFrag = new DisplayFrag();
            displayFrag = putBundle(displayFrag);
            fragMan.beginTransaction().add(R.id.hook2, displayFrag, Keys.TAG_DISPLAY_FRAG).commit();
        } else {
            fragMan.beginTransaction().replace(R.id.hook2, displayFrag, Keys.TAG_DISPLAY_FRAG)
                    .commit();
        }

    }

    private DisplayFrag putBundle(DisplayFrag displayFrag) {
        Bundle bundle = new Bundle();
        bundle.putString(Keys.NAME, ((EditText) findViewById(R.id.etName)).getText().toString());
        bundle.putString(Keys.PHONE, ((EditText) findViewById(R.id.etPhone)).getText().toString());
        displayFrag.setArguments(bundle);
        return displayFrag;
    }
}

(提交响应另一个片段上的按钮,该按钮行为正确)。

4

3 回答 3

0

找到了解决方案:根本不需要 .replace() 片段。这是 MainActivity 和 DisplayFrag 的新代码:

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        FragmentManager fragMan = getSupportFragmentManager();
        InputFrag inputFrag = (InputFrag) fragMan.findFragmentByTag(Keys.TAG_INPUT_FRAG);
        if (inputFrag == null) {
            fragMan.beginTransaction().add(R.id.hook1, new InputFrag(), Keys.TAG_INPUT_FRAG)
                    .commit();
        }

        DisplayFrag displayFrag = (DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
        if (displayFrag == null) {
            fragMan.beginTransaction().add(R.id.hook2, new DisplayFrag(), Keys.TAG_DISPLAY_FRAG)
                    .commit();
        }
    }

    public void submit(View v) {
        String name = ((EditText) findViewById(R.id.etName)).getText().toString();
        String phone = ((EditText) findViewById(R.id.etPhone)).getText().toString();
        if (!"".matches(name) && !"".matches(phone)) {
            FragmentManager fragMan = getSupportFragmentManager();
            DisplayFrag displayFrag =
                    (DisplayFrag) fragMan.findFragmentByTag(Keys.TAG_DISPLAY_FRAG);
            if (displayFrag != null) {
                displayFrag.display(name, phone);
            }
        }
    }
}

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class DisplayFrag extends Fragment {

    @Override
    public View
            onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.display_frag, container, false);
        return view;
    }

    public void display(String name, String phone) {
        TextView tvDisplay = (TextView) getActivity().findViewById(R.id.tvDisplay);
        tvDisplay.setText(name + ", how are you? your phone number is " + phone);
    }

}

暂时接受我的回答。如果有人有任何其他答案、建议或知识要分享,我会很乐意接受不同的答案。

于 2013-01-10T08:25:41.530 回答
0

这是因为您的活动每次都会创建一个新片段。你可以简单地通过检查saveInstanceState 来做到这一点。希望我的例子能帮助你感觉更好。

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null){
        fragMan.beginTransaction().add(R.id.hook1, new InputFrag())
                .commit();

        fragMan.beginTransaction().add(R.id.hook2, new DisplayFrag())
                .commit();
    }

}

并将某些错误发生在某些版本上。

@Override protected void onSaveInstanceState(Bundle outState) {
    // first saving my state, so the bundle wont be empty.
    // http://code.google.com/p/android/issues/detail?id=19917
    outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
    super.onSaveInstanceState(outState);
}

片段的生命周期与活动不同,设置为

android:configChanges="orientation

不会真正解决问题。

于 2014-06-06T10:37:48.710 回答
-1

修改 AndroidManifest.xml 中的活动以通过将其添加到您的活动/活动来覆盖方向更改行为:

android:configChanges="orientation|keyboardHidden"

它应该看起来像这样:

<activity android:name=".activity.LoginActivity"
          android:configChanges="orientation|keyboardHidden"
          android:label="@string/app_name">
</activity>
于 2013-02-05T10:03:03.457 回答