重新启动应用程序后,android textview被加倍/镜像/屏蔽

时间:2014-07-14 17:25:09

标签: android textview

enter image description here 这是在以下情况之后发生的事情:

1)我打开我的应用程序,执行一些常规操作。 2)关闭我的应用程序,(杀死碎片和活动)。 3)从应用程序抽屉重新打开我的应用程序。

我感觉这是我在生命周期事件中所做的事情。 有没有人遇到过这个?怎么了?

活动代码(视图初始化)

public class PlayClient extends Activity {
public static Context ctx = null;

private String TAG = "PlayClient Activity";

private Fragment main;
private Fragment mSoundCloudFrag;
private MediaControlsComponent mdc;

private FragmentManager fm;

private String[] mNavigationItems;
private ListView mDrawerList;
private DrawerLayout mDrawer;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_layout);
    ctx = this;

    // Sound Cloud fragment
    mSoundCloudFrag = new SoundCloudFragment();

    // Add just the main player fragment
    main = new Play_Main();
    mdc = new MediaControlsComponent();


    fm = getFragmentManager();
    FragmentTransaction ft = fm.beginTransaction();

    ft.add(R.id.frameContainer, main, "main");
    ft.setCustomAnimations(R.animator.fade_in, android.R.animator.fade_out);
    ft.add(R.id.mediaControllerFrame, mdc);
    ft.commit();
    getFragmentManager().executePendingTransactions();


    // Navigation Drawer
    mNavigationItems = getResources().getStringArray(R.array.nav_drawer);
    mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.left_drawer);

片段代码

public class Play_Main extends Fragment implements IListener {

    private static final String TAG = "Play_Main_Fragment";
    public static CountDownLatch mCountDown = new CountDownLatch(1);

    private Typeface roboto;

    // Views
    private TextView tv1;
    private TextView pullToConnect;
    private Button connectButton;
    private SwipeRefreshLayout mSwipeLayout;


    // Object Instances
    private Play_Main mainFrag;
    private MessageManager messageManager;

    // Animation
    private Animation fadeIn;
    private Animation fadeOut;

    // Fields
    private String isConnectedText;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.main_ui_frame, container, false);
        super.onCreateView(inflater, container, null);

        mainFrag = this;
        final Gson jsonMaker = new Gson();

        roboto = Typeface.createFromAsset(getActivity().getAssets(), "fonts/robotot.ttf");

        fadeOut = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_out);
        fadeIn = AnimationUtils.loadAnimation(getActivity(), R.anim.fade_in);

        messageManager = MessageManager.Instance();
        // Register as a general listener
        messageManager.RegisterListener(mainFrag);
        // Register as the UI to interact with
        messageManager.registerUI(mainFrag);

        pullToConnect = (TextView) view.findViewById(R.id.pullToConnect);
        pullToConnect.setTypeface(roboto);

        tv1 = (TextView) view.findViewById(R.id.tv1);
        tv1.setTypeface(roboto);
        tv1.setText("Connect and start playing");


        mSwipeLayout = (SwipeRefreshLayout) view.findViewById(R.id.swipe_container);
        mSwipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

活动XML:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="100"
        android:background="@color/darker"
        android:orientation="vertical">


        <FrameLayout
            android:id="@+id/frameContainer"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:layout_weight="80"></FrameLayout>

        <FrameLayout
            android:id="@+id/mediaControllerFrame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_alignParentLeft="true"
            android:layout_weight="20"></FrameLayout>

    </LinearLayout>


    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="200dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#111"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="1dp" />
</android.support.v4.widget.DrawerLayout>

片段的XML

<?xml version="1.0" encoding="utf-8"?>


<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/swipe_container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:background="@android:color/transparent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:orientation="vertical">

                <TextView
                    android:id="@+id/pullToConnect"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_gravity="center"
                    android:layout_marginTop="10dp"
                    android:gravity="center"
                    android:text="@string/pull_to_connect"
                    android:textColor="@android:color/white"
                    android:textSize="23dp" />

            </LinearLayout>


            <RelativeLayout
                android:id="@+id/relative"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent">


                <TextView
                    android:id="@+id/tv1"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_centerHorizontal="true"
                    android:layout_marginTop="24dp"
                    android:gravity="center_horizontal"
                    android:padding="15dp"
                    android:textColor="@android:color/white"
                    android:textSize="30dp" />

            </RelativeLayout>
        </LinearLayout>
    </ScrollView>
</android.support.v4.widget.SwipeRefreshLayout>

1 个答案:

答案 0 :(得分:1)

fm = getFragmentManager();
FragmentTransaction ft = fm.beginTransaction();

ft.add(R.id.frameContainer, main, "main");
ft.setCustomAnimations(R.animator.fade_in, android.R.animator.fade_out);
ft.add(R.id.mediaControllerFrame, mdc);
ft.commit();
getFragmentManager().executePendingTransactions();

您只应在第一次调用onCreate()时执行此片段初始化。 FragmentManager处理在配置更改中保留您的片段,因此如果您要旋转屏幕,将再次调用onCreate(),您将添加main和{{1}的另一个实例每次都在旧的之上。

而是使用mdc将片段初始化代码包装在空检查中。如果savedInstanceState为空,则表示它是您的活动的第一次创建:

savedInstanceState