相关疑难解决方法(0)

一劳永逸,如何在后台堆栈中正确保存Fragments的实例状态?

我在SO上发现了许多类似问题的实例,但遗憾的是没有答案符合我的要求.

我有纵向和横向的不同布局,我正在使用后台堆栈,这两个都阻止我setRetainState()使用配置更改例程使用和技巧.

我在TextViews中向用户显示某些信息,这些信息不会保存在默认处理程序中.仅使用活动编写我的应用程序时,以下工作正常:

TextView vstup;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.whatever);
    vstup = (TextView)findViewById(R.id.whatever);
    /* (...) */
}

@Override
public void onSaveInstanceState(Bundle state) {
    super.onSaveInstanceState(state);
    state.putCharSequence(App.VSTUP, vstup.getText());
}

@Override
public void onRestoreInstanceState(Bundle state) {
    super.onRestoreInstanceState(state);
    vstup.setText(state.getCharSequence(App.VSTUP));
}
Run Code Online (Sandbox Code Playgroud)

使用Fragments,这仅适用于非常特定的情况.具体来说,可怕的破坏是替换片段,将其放入后栈,然后在显示新片段时旋转屏幕.根据我的理解,旧片段onSaveInstanceState()在被替换时没有接收到调用但是以某种方式Activity与之相关联,并且此方法稍后在其View不再存在时调用,因此将我TextView的任何结果查找为a NullPointerException.

另外,我发现保留对我的引用对于s TextViews来说并不是一个好主意Fragment,即使它是正常Activity的.在这种情况下,onSaveInstanceState()实际上保存状态但是如果我在隐藏片段时旋转屏幕两次,则问题会重新出现,因为它onCreateView()不会在新实例中被调用.

我想把状态保存onDestroyView()到某个Bundle类型的类成员元素中(它实际上是更多的数据,而不仅仅是一个TextView)并保存,onSaveInstanceState()但还有其他缺点.首先,如果该片段当前显示,调用两个函数的顺序是相反的,所以我需要考虑两种不同的情况.必须有一个更清洁,更正确的解决方案!

android android-fragments

465
推荐指数
5
解决办法
27万
查看次数

FragmentTabHost - 没有标签为null的标签

前几天,我实现TabHostFramgentFragment一个NavigationDrawer,和我面临的是以下错误的问题:

java.lang.IllegalStateException:没有标记null的标签

事情是所有的工作完美,因为我的第一个项目列表NavigationDrawer是我的TabHostFragment,所以它工作完美,但问题是当我进入第二个项目的例子然后我想回到第一个项目,每次我试试它崩溃了.

我在寻找随处可见,在这里SO,code.google.com等...我仍然没有得到正确的答案.

tab_host_test_2.xml看起来像:

  <android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0" />

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1" />
    </LinearLayout>

</android.support.v4.app.FragmentTabHost>
Run Code Online (Sandbox Code Playgroud)

activity_main.xml在哪里看到我的NavigationDrawer样子:

 <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">

    <!-- Framelayout to display Fragments -->
    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />


    <!-- Listview to display slider menu …
Run Code Online (Sandbox Code Playgroud)

java android android-layout android-tabhost android-viewpager

17
推荐指数
2
解决办法
1万
查看次数

Android - 使用AnimatorSet缩放动画

官方缩放视图教程使用a AnimatorSet来放大View.随着视野的扩大,它会产生向下运动的错觉.之后,AnimatorSet只是向后重放以创建缩小的幻觉.

放大向下移动 我需要实现的是与此完全相反的.我需要从扩展视图开始,然后将其缩小为一个向上移动的较小视图:

向上移动缩小 我似乎不能在示例中使用反转代码.该示例假定您首先放大视图并展开它,然后将其缩小回原始缩略图图标.

这是我到目前为止所尝试的内容.我的XML布局是

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#1999da">             

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:orientation="horizontal"
        android:layout_gravity="center"
        android:gravity="center">

        <!-- The final shrunk image -->

        <ImageView
            android:id="@+id/thumb_button_1"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:layout_marginRight="1dp"
            android:visibility="invisible"/>

    </LinearLayout>

</LinearLayout>

<!-- The initial expanded image that needs to be shrunk -->

<ImageView
    android:id="@+id/expanded_image"
    android:layout_width="wrap_content"
    android:layout_height="125dp"
    android:layout_gravity="center"
    android:src="@drawable/title_logo_expanded"
    android:scaleType="centerCrop"/>

</FrameLayout>
Run Code Online (Sandbox Code Playgroud)

这是执行缩小操作的方法.我基本上试图颠倒教程中的过程:

private void zoomImageFromThumbReverse(final View expandedImageView, int imageResId, final int duration) {
    // If there's an animation in …
Run Code Online (Sandbox Code Playgroud)

java android android-animation objectanimator viewpropertyanimator

15
推荐指数
1
解决办法
2926
查看次数