简体   繁体   English

方向更改后还原视图

[英]Restoring a view after orientation change

I have a Fragment that contain EditText and ImageView that's need to be retain across configuration changes so I tried to save the text and the bitmap via onSaveInstanceState . 我有一个包含EditTextImageViewFragment ,需要在配置更改期间保留它们,因此我尝试通过onSaveInstanceState保存文本和位图。

But when I rotate the device problems start. 但是当我旋转设备时,问题开始了。

the text i saved overlap with the hint of EditText and the bitmap overlap with the original placeholder of the ImageView . 我保存的文本与EditText的提示重叠,而位图与ImageView的原始占位符重叠。

I've tried to do the restore from various methods ( onCreateView , onViewCreated and onViewStateRestored ). 我试图通过各种方法( onCreateViewonViewCreatedonViewStateRestored )进行还原。 But still the same problem remain. 但是仍然存在相同的问题。

so instead of onSaveInstanceState I used setRetainInstance(true) and the problem remain with the text overlap the hint in EditText . 因此,我使用了setRetainInstance(true)而不是onSaveInstanceState ,问题仍然存在,因为文本与EditText的提示重叠。

So where did I do wrong? 那我在哪里做错了? AND how to solve this. 以及如何解决这个问题。

this is the EditText : 这是EditText

<EditText
    android:id="@+id/username_editext"
    android:layout_centerVertical="true"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="@string/enter_ur_name"
    android:inputType="text"
    android:lines="1"
    android:padding="8dp"/>

and this is the ImageView : 这是ImageView

<de.hdodenhof.circleimageview.CircleImageView
    android:id="@+id/profile_image"
    android:layout_above="@id/username_editext"
    android:layout_centerHorizontal="true"
    android:layout_width="100dp"
    android:layout_height="100dp"
    android:src="@drawable/profile"
    />

and here is the code for save & restore via bundle : 这是通过bundle保存和还原的代码:

public void onViewStateRestored(@Nullable Bundle savedInstanceState) {
    super.onViewStateRestored(savedInstanceState);

    if(savedInstanceState != null){
        Bitmap map = (Bitmap) savedInstanceState.getParcelable(BITMAP_KEY);
        if(map != null)
            mProfile.setImageBitmap(map);
        String name = savedInstanceState.getString(USERNAME_KEY);
        if(name != null)
            mUsernameEditext.setText(name);
    }
}

@Override
public void onSaveInstanceState(@NonNull Bundle outState) {
    if(isPhotoUploaded) {
        Bitmap map = ((BitmapDrawable)mProfile.getDrawable()).getBitmap();
        outState.putParcelable(BITMAP_KEY, map);
    }

    if(!mUsernameEditext.getText().toString().trim().equals("")){
        outState.putString(USERNAME_KEY , mUsernameEditext.getText().toString());
    }
}

Edit 1: This is the complete layout code: Edit 1:这是完整的布局代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".UsernameFragment">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profile_image"
        android:layout_above="@id/username_text_input_layout"
        android:layout_centerHorizontal="true"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@drawable/profile"
        />

    <android.support.design.widget.TextInputLayout
        android:layout_centerVertical="true"
        android:id="@+id/username_text_input_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="@string/enter_ur_name"
        >
        <EditText
            android:id="@+id/username_editext"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:inputType="text"
            android:lines="1"
            android:padding="8dp"/>
    </android.support.design.widget.TextInputLayout>

    <Button
        android:id="@+id/upload_photo_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/upload_photo"
        android:layout_centerHorizontal="true"
        android:layout_below="@id/username_text_input_layout"/>

    <Button
        android:id="@+id/next_btn"
        android:layout_below="@id/upload_photo_btn"
        android:layout_centerHorizontal="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"/>

</RelativeLayout>

I have found that if I use tag instead of container's id the issue is gone. 我发现,如果我使用标签而不是容器的ID,问题就消失了。

This is the code before to build the fragment : 这是构建fragment之前的代码:

    FragmentManager manager = getSupportFragmentManager();
    if (manager.findFragmentById(R.id.container) == null) {
        usernameFragment = UsernameFragment.newInstance();
        manager.beginTransaction()
                .add(R.id.container , usernameFragment)
                .commit();
    }

And this is the code now: 这是现在的代码:

    FragmentManager manager = getSupportFragmentManager();
    if (manager.findFragmentByTag("user_name") == null) {
        usernameFragment = UsernameFragment.newInstance();
        manager.beginTransaction()
                .add(R.id.container , usernameFragment , "user_name")
                .commit();
    }

I still don't know why the first code make overlap in views if rotated. 我仍然不知道为什么旋转后第一个代码在视图中会重叠。 and returned null if I call getter of any view in fragment 如果我在片段中调用任何视图的getter,则返回null

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

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