进一步了解setRetainInstance(true) [英] Further understanding setRetainInstance(true)

查看:171
本文介绍了进一步了解setRetainInstance(true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

片段中调用 setRetainInstance(true)时,会发生什么?文件实际上不存在,这似乎是一个非常重要的功能。具体来说,我想知道这个序列有多少(我所做的)是真的:



  1. 用户

  2. 片段与活动 Fragment.onDetach()被调用。

  3. 活动被销毁; Activity.onDestroy()被调用。

  4. 活动 java对象是删除(可能时由GC)。

  5. 创建新对象活动它的构造函数和 onCreate()被调用。

  6. Activity.onCreate()我们有 setContentView(...),它设置一个包含片段的布局,或者我们使用 FragmentTransaction
    添加一个片段。

  7. 我真的不知道这一点,但我认为android很聪明,可以找到旧片段,并调用 Fragment.onAttach()
    重新连接到新的活动

  8. Next或者之前?谁知道?) Activity.onResume()被调用。


那是正确的吗?即使我明确地使用了 FragmentTransaction.add(新的MyFragment(),...),Android的智能足以找到旧的片段。如果是,如何避免在 onCreate()中添加另一个片段?我需要这样做吗?:

  if(getSupportFragmentManager()。findFragmentByTag(foo)== null) 
{
FragmentTransaction ft = getSupportFragmentManager()。beginTransaction();
ft.add(new FooFragment(),foo)。commit();
}

文档中为什么没有这些?不,只是开玩笑不要回答!

解决方案

好的,也许我对Android文档太粗暴了,因为它确实有一些有用的信息,但遗憾的是没有一个从 setRetainInstance()链接。从关于片段的页面


注意:每个片段需要一个唯一的标识符,系统可以使用
来恢复片段,如果活动重新启动(以及哪些
可以用来捕获片段来执行事务,例如作为
删除它)。有三种方式可以为片段提供ID:




  • 提供具有唯一ID的android:id属性。

  • 使用唯一的字符串提供android:tag属性。

  • 如果您不提供前两个,系统将使用容器视图的ID。 >

这强烈暗示如果你做 setContentView(R.layout.whatever) Activity.onCreated()中,该布局包含带有 setRetainInstance(true)的片段,那么当重新创建活动时,将使用其id或标签再次搜索。



其次,对于无UI的片段,它说明

$ b $要添加没有UI的片段,请使用add(Fragment,String)添加活动
中的片段(为片段提供唯一的字符串标签)
片段,而不是视图ID)。这会添加片段,但是因为
它与活动布局中的视图没有关联,所以它不会
接收对onCreateView()的调用。所以你不需要实现那个
方法。


文档链接到一个很好的例子 - FragmentRetainInstance.java 我以下转载了您的方便。它确实是我猜测的答案在我的问题( if(... findFragmentByTag()== null){... )。



最后,我创建了自己的测试活动,以查看哪些函数被调用。它输出这个,当你开始纵向旋转到风景。代码如下。



(这是一些修改,使其更容易阅读。)

  TestActivity @ 415a4a30:this()
TestActivity @ 415a4a30:onCreate()
TestActivity @ 415a4a30:找不到现有片段。
TestFragment {41583008}:this()TestFragment {41583008}
TestFragment {41583008}:onAttach(TestActivity @ 415a4a30)
TestFragment {41583008}:onCreate()
TestFragment {41583008 }:onCreateView()
TestFragment {41583008}:onActivityCreated()
TestActivity @ 415a4a30:onStart()
TestFragment {41583008}:onStart()
TestActivity @ 415a4a30:onResume )
TestFragment {41583008}:onResume()

< rotate device>

TestFragment {41583008}:onPause()
TestActivity @ 415a4a30:onPause()
TestFragment {41583008}:onStop()
TestActivity @ 415a4a30:onStop()
TestFragment {41583008}:onDestroyView()
TestFragment {41583008}:onDetach()
TestActivity @ 415a4a30:onDestroy()
TestActivity @ 415a3380:this()
TestFragment {41583008}:onAttach(TestActivity @ 415a3380)
TestActivity @ 415a3380:onCreate()
TestActivity @ 415a3380:找到现有的片段。
TestFragment {41583008}:onCreateView()
TestFragment {41583008}:onActivityCreated()
TestActivity @ 415a3380:onStart()
TestFragment {41583008}:onStart()
TestActivity @ 415a3380:onResume()
TestFragment {41583008}:onResume()

请注意,Android文档是错误的:无UI的片段 接收到 onCreateView()的调用,但可以自由返回 null



源代码 TestActivity / TestFragment



  import android.app.Activity; 
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.concentriclivers.ss.R;

//了解Android生命周期事件的活动。
public class TestActivity extends Activity
{
private static final String TAG = TestActivity.class.getSimpleName();

public TestActivity()
{
super();
Log.d(TAG,this +:this());
}

protected void finalize()throws Throwable
{
super.finalize();
Log.d(TAG,this +:finalize());
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG,this +:onCreate());


TextView tv = new TextView(this);
tv.setText(Hello world);
setContentView(tv);

if(getFragmentManager()。findFragmentByTag(test_fragment)== null)
{
Log.d(TAG,this +:找不到现有的片段) ;
FragmentTransaction ft = getFragmentManager()。beginTransaction();
ft.add(new TestFragment(),test_fragment)。commit();
}
else
{
Log.d(TAG,this +:找到的现有片段);
}
}

@Override
public void onStart()
{
super.onStart();
Log.d(TAG,this +:onStart());
}

@Override
public void onResume()
{
super.onResume();
Log.d(TAG,this +:onResume());
}

@Override
public void onPause()
{
super.onPause();
Log.d(TAG,this +:onPause());
}

@Override
public void onStop()
{
super.onStop();
Log.d(TAG,this +:onStop());
}

@Override
public void onDestroy()
{
super.onDestroy();
Log.d(TAG,this +:onDestroy());
}




public static class TestFragment extends Fragment
{
private static final String TAG = TestFragment.class。 getSimpleName();

public TestFragment()
{
超级();
Log.d(TAG,this +:this()+ this);
}

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.d(TAG,this +:onCreate());
setRetainInstance(true);
}

@Override
public void onAttach(final Activity activity)
{
super.onAttach(activity);
Log.d(TAG,this +:onAttach(+ activity +));
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
Log.d(TAG,this +:onActivityCreated());
}

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup容器,Bundle savedInstanceState)
{
Log.d(TAG,this + :onCreateView());
返回null;
}

@Override
public void onViewCreated(View view,Bundle savedInstanceState)
{
super.onViewCreated(view,savedInstanceState);
Log.d(TAG,this +:onViewCreated());
}

@Override
public void onDestroyView()
{
super.onDestroyView();
Log.d(TAG,this +:onDestroyView());
}

@Override
public void onDetach()
{
super.onDetach();
Log.d(TAG,this +:onDetach());
}

@Override
public void onStart()
{
super.onStart();
Log.d(TAG,this +:onStart());
}

@Override
public void onResume()
{
super.onResume();
Log.d(TAG,this +:onResume());
}

@Override
public void onPause()
{
super.onPause();
Log.d(TAG,this +:onPause());
}

@Override
public void onStop()
{
super.onStop();
Log.d(TAG,this +:onStop());
}

@Override
public void onDestroy()
{
super.onDestroy();
Log.d(TAG,this +:onDestroy());
}
}

}



源代码对于 FragmentRetainInstance.java (API 16):



  / * 
*版权所有(C)2010 Android开源项目
*
*根据Apache许可证版本2.0(许可证)许可;
*除非符合许可证,否则您不得使用此文件。
*您可以在
*
* http://www.apache.org/licenses/LICENSE-2.0
*
*获得许可证副本根据适用法律要求或以书面形式同意,根据许可证分发的软件
*以现状为基础分发,
*,不提供任何明示或暗示的保证或条件。
*有关许可的特定语言和
*限制,请参阅许可证。
* /

包com.example.android.apis.app;

import com.example.android.apis.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

/ **
*此示例显示如果活动需要$ b时,如何使用片段轻松地在活动实例中传播状态
*(如线程) $ b *由于例如配置更改而重新启动。这比使用原始Activity.onRetainNonConfiguratinInstance()API更容易
*。
* /
public class FragmentRetainInstance extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//首次初始化,创建UI。
if(savedInstanceState == null){
getFragmentManager()。beginTransaction()。add(android.R.id.content,
new UiFragment())。
}
}

/ **
*这是一个片段,显示从保留片段中的工作完成
*更新的UI。
* /
public static class UiFragment extends Fragment {
RetainedFragment mWorkFragment;

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup容器,
Bundle savedInstanceState){
查看v = inflater.inflate(R.layout.fragment_retain_instance,container ,假);

//观看按钮点击。
按钮按钮=(Button)v.findViewById(R.id.restart);
button.setOnClickListener(new OnClickListener(){
public void onClick(View v){
mWorkFragment.restart();
}
});

return v;
}

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

FragmentManager fm = getFragmentManager();

//检查我们是否保留了worker片段。
mWorkFragment =(RetainedFragment)fm.findFragmentByTag(work);

//如果没有保留(或第一次运行),我们需要创建它。
if(mWorkFragment == null){
mWorkFragment = new RetainedFragment();
//告诉它是否正在使用它。
mWorkFragment.setTargetFragment(this,0);
fm.beginTransaction()。add(mWorkFragment,work)。commit();
}
}

}

/ **
*这是Fragment实现,将保留在
*活动实例。它代表一些正在进行的工作,这里有一个线程
*我们有一个增加一个进度指示器。
* /
public static class RetainedFragment extends Fragment {
ProgressBar mProgressBar;
int mPosition;
boolean mReady = false;
boolean mQuiting = false;

/ **
*这是我们工作的线程。它坐在循环中运行
*进度,直到达到顶部,然后停止等待。
* /
最后线程mThread = new Thread(){
@Override
public void run(){
//我们会在后面确定真正的价值。
int max = 10000;

//这个线程几乎永远运行。
while(true){

//用UI更新我们的共享状态。
synchronized(this){
//如果UI还没有准备好
//或者它已经完成了工作,我们的线程就会停止。
while(!mReady || mPosition> = max){
if(mQuiting){
return;
}
try {
wait();
} catch(InterruptedException e){
}
}

//现在更新进度。请注意,重要的是,
//我们触摸锁定的进度条,所以
//不会消失在我们身上。
mPosition ++;
max = mProgressBar.getMax();
mProgressBar.setProgress(mPosition);
}

//通常我们会做一些工作,但是在这里放一个kludge
//假装就像我们一样。
synchronized(this){
try {
wait(50);
} catch(InterruptedException e){
}
}
}
}
};

/ **
*片段初始化。我们要保留的方式,
*启动我们的线程。
* /
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);

//在配置更改期间,告诉框架尝试将此片段保留在
//周围。
setRetainInstance(true);

//启动工作线程。
mThread.Start();
}

/ **
*当片段的活动准备好时,在
*的内容视图已经安装之后,这是被调用的;它在
*之后被称为初始片段创建,并且片段被重新附加
*之后被称为新的活动。
* /
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);

//从目标的视图层次结构中检索进度条。
mProgressBar =(ProgressBar)getTargetFragment()。getView()。findViewById(
R.id.progress_horizo​​ntal);

//我们准备好线程去了。
synchronized(mThread){
mReady = true;
mThread.notify();
}
}

/ **
*当片断消失时,这是被调用的。当片段在活动实例之间传播时,它不会被称为
*。
* /
@Override
public void onDestroy(){
//使线程消失。
synchronized(mThread){
mReady = false;
mQuiting = true;
mThread.notify();
}

super.onDestroy();
}

/ **
*这是在片段与
*当前活动实例分离之前调用的。
* /
@Override
public void onDetach(){
//此片段与其活动分离。我们需要
//来确保其线程在从此函数返回后不会触摸任何活动
//状态。
synchronized(mThread){
mProgressBar = null;
mReady = false;
mThread.notify();
}

super.onDetach();
}

/ **
*我们的UI重新启动进度线程的API。
* /
public void restart(){
synchronized(mThread){
mPosition = 0;
mThread.notify();
}
}
}
}


What exactly happens when you call setRetainInstance(true) on a Fragment? The documentation is virtually non-existent and this seems like a very important function. Specifically I want to know how much of this sequence (that I made up) is true:

  1. The user rotates the device.
  2. The fragment is detached from the Activity and Fragment.onDetach() is called.
  3. The activity is destroyed; Activity.onDestroy() is called.
  4. The Activity java object is deleted (when possible, by the GC).
  5. A new Activity java object is created; its constructor, and onCreate() are called.
  6. In Activity.onCreate() we either have setContentView(...) which sets a layout containing a fragment, or we use FragmentTransaction to add a fragment.
  7. I'm really not sure about this, but I assume that android is smart enough to find the old fragment, and call Fragment.onAttach() to reattach it to the new Activity
  8. Next (or before? who knows?) Activity.onResume() is called.

So is that correct? Is Android smart enough to find the old fragment, even if I explicitly use FragmentTransaction.add(new MyFragment(), ...) the first time? And if so, how do I avoid adding another fragment in onCreate()? Do I need to do something like this?:

    if (getSupportFragmentManager().findFragmentByTag("foo") == null)
    {
        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(new FooFragment(), "foo").commit();
    }

Why is none of this in the documentation? Ha no, just kidding don't answer that!

解决方案

Ok, perhaps I was slightly too harsh on the Android documentation, because it does have some useful information, but sadly none of it is linked from setRetainInstance(). From the page about fragments

Note: Each fragment requires a unique identifier that the system can use to restore the fragment if the activity is restarted (and which you can use to capture the fragment to perform transactions, such as remove it). There are three ways to provide an ID for a fragment:

  • Supply the android:id attribute with a unique ID.
  • Supply the android:tag attribute with a unique string.
  • If you provide neither of the previous two, the system uses the ID of the container view.

This strongly implies that if you do setContentView(R.layout.whatever) in Activity.onCreated() and that layout contains a fragment with setRetainInstance(true), then when the activity is recreated it will be searched for again using its id or tag.

Secondly, for UI-less fragments, it states

To add a fragment without a UI, add the fragment from the activity using add(Fragment, String) (supplying a unique string "tag" for the fragment, rather than a view ID). This adds the fragment, but, because it's not associated with a view in the activity layout, it does not receive a call to onCreateView(). So you don't need to implement that method.

And the docs link to a very good example - FragmentRetainInstance.java which I have reproduced below for your convenience. It does exactly what I speculated was the answer in my question (if (...findFragmentByTag() == null) { ...).

Finally, I created my own test activity to see exactly what functions are called. It outputs this, when you start in portrait and rotate to landscape. The code is below.

(This is edited a bit to make it easier to read.)

TestActivity@415a4a30: this()
TestActivity@415a4a30: onCreate()
TestActivity@415a4a30: Existing fragment not found.
TestFragment{41583008}: this() TestFragment{41583008}
TestFragment{41583008}: onAttach(TestActivity@415a4a30)
TestFragment{41583008}: onCreate()
TestFragment{41583008}: onCreateView()
TestFragment{41583008}: onActivityCreated()
TestActivity@415a4a30: onStart()
TestFragment{41583008}: onStart()
TestActivity@415a4a30: onResume()
TestFragment{41583008}: onResume()

<rotate device>

TestFragment{41583008}: onPause()
TestActivity@415a4a30: onPause()
TestFragment{41583008}: onStop()
TestActivity@415a4a30: onStop()
TestFragment{41583008}: onDestroyView()
TestFragment{41583008}: onDetach()
TestActivity@415a4a30: onDestroy()
TestActivity@415a3380: this()
TestFragment{41583008}: onAttach(TestActivity@415a3380)
TestActivity@415a3380: onCreate()
TestActivity@415a3380: Existing fragment found.
TestFragment{41583008}: onCreateView()
TestFragment{41583008}: onActivityCreated()
TestActivity@415a3380: onStart()
TestFragment{41583008}: onStart()
TestActivity@415a3380: onResume()
TestFragment{41583008}: onResume()

Note that the Android documentation is wrong: the UI-less fragment does receive a call to onCreateView() but it is free to return null.

Source code for TestActivity/TestFragment

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.concentriclivers.ss.R;

// An activity for understanding Android lifecycle events.
public class TestActivity extends Activity
{
    private static final String TAG = TestActivity.class.getSimpleName();

    public TestActivity()
    {
        super();
        Log.d(TAG, this + ": this()");
    }

    protected void finalize() throws Throwable
    {
        super.finalize();
        Log.d(TAG, this + ": finalize()");
    }

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        Log.d(TAG, this + ": onCreate()");


        TextView tv = new TextView(this);
        tv.setText("Hello world");
        setContentView(tv);

        if (getFragmentManager().findFragmentByTag("test_fragment") == null)
        {
            Log.d(TAG, this + ": Existing fragment not found.");
            FragmentTransaction ft = getFragmentManager().beginTransaction();
            ft.add(new TestFragment(), "test_fragment").commit();
        }
        else
        {
            Log.d(TAG, this + ": Existing fragment found.");
        }
    }

    @Override
    public void onStart()
    {
        super.onStart();
        Log.d(TAG, this + ": onStart()");
    }

    @Override
    public void onResume()
    {
        super.onResume();
        Log.d(TAG, this + ": onResume()");
    }

    @Override
    public void onPause()
    {
        super.onPause();
        Log.d(TAG, this + ": onPause()");
    }

    @Override
    public void onStop()
    {
        super.onStop();
        Log.d(TAG, this + ": onStop()");
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
        Log.d(TAG, this + ": onDestroy()");
    }




    public static class TestFragment extends Fragment
    {
        private static final String TAG = TestFragment.class.getSimpleName();

        public TestFragment()
        {
            super();
            Log.d(TAG,  this + ": this() " + this);
        }

        @Override
        public void onCreate(Bundle savedInstanceState)
        {
            super.onCreate(savedInstanceState);
            Log.d(TAG, this + ": onCreate()");
            setRetainInstance(true);
        }

        @Override
        public void onAttach(final Activity activity)
        {
            super.onAttach(activity);
            Log.d(TAG, this + ": onAttach(" + activity + ")");
        }

        @Override
        public void onActivityCreated(Bundle savedInstanceState)
        {
            super.onActivityCreated(savedInstanceState);
            Log.d(TAG, this + ": onActivityCreated()");
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
        {
            Log.d(TAG, this + ": onCreateView()");
            return null;
        }

        @Override
        public void onViewCreated(View view, Bundle savedInstanceState)
        {
            super.onViewCreated(view, savedInstanceState);
            Log.d(TAG, this + ": onViewCreated()");
        }

        @Override
        public void onDestroyView()
        {
            super.onDestroyView();
            Log.d(TAG, this + ": onDestroyView()");
        }

        @Override
        public void onDetach()
        {
            super.onDetach();
            Log.d(TAG, this + ": onDetach()");
        }

        @Override
        public void onStart()
        {
            super.onStart();
            Log.d(TAG, this + ": onStart()");
        }

        @Override
        public void onResume()
        {
            super.onResume();
            Log.d(TAG, this + ": onResume()");
        }

        @Override
        public void onPause()
        {
            super.onPause();
            Log.d(TAG, this + ": onPause()");
        }

        @Override
        public void onStop()
        {
            super.onStop();
            Log.d(TAG, this + ": onStop()");
        }

        @Override
        public void onDestroy()
        {
            super.onDestroy();
            Log.d(TAG, this + ": onDestroy()");
        }
    }

}

Source code for FragmentRetainInstance.java (as of API 16):

/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.example.android.apis.app;

import com.example.android.apis.R;

import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;

/**
 * This example shows how you can use a Fragment to easily propagate state
 * (such as threads) across activity instances when an activity needs to be
 * restarted due to, for example, a configuration change.  This is a lot
 * easier than using the raw Activity.onRetainNonConfiguratinInstance() API.
 */
public class FragmentRetainInstance extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // First time init, create the UI.
        if (savedInstanceState == null) {
            getFragmentManager().beginTransaction().add(android.R.id.content,
                    new UiFragment()).commit();
        }
    }

    /**
     * This is a fragment showing UI that will be updated from work done
     * in the retained fragment.
     */
    public static class UiFragment extends Fragment {
        RetainedFragment mWorkFragment;

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

            // Watch for button clicks.
            Button button = (Button)v.findViewById(R.id.restart);
            button.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    mWorkFragment.restart();
                }
            });

            return v;
        }

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

            FragmentManager fm = getFragmentManager();

            // Check to see if we have retained the worker fragment.
            mWorkFragment = (RetainedFragment)fm.findFragmentByTag("work");

            // If not retained (or first time running), we need to create it.
            if (mWorkFragment == null) {
                mWorkFragment = new RetainedFragment();
                // Tell it who it is working with.
                mWorkFragment.setTargetFragment(this, 0);
                fm.beginTransaction().add(mWorkFragment, "work").commit();
            }
        }

    }

    /**
     * This is the Fragment implementation that will be retained across
     * activity instances.  It represents some ongoing work, here a thread
     * we have that sits around incrementing a progress indicator.
     */
    public static class RetainedFragment extends Fragment {
        ProgressBar mProgressBar;
        int mPosition;
        boolean mReady = false;
        boolean mQuiting = false;

        /**
         * This is the thread that will do our work.  It sits in a loop running
         * the progress up until it has reached the top, then stops and waits.
         */
        final Thread mThread = new Thread() {
            @Override
            public void run() {
                // We'll figure the real value out later.
                int max = 10000;

                // This thread runs almost forever.
                while (true) {

                    // Update our shared state with the UI.
                    synchronized (this) {
                        // Our thread is stopped if the UI is not ready
                        // or it has completed its work.
                        while (!mReady || mPosition >= max) {
                            if (mQuiting) {
                                return;
                            }
                            try {
                                wait();
                            } catch (InterruptedException e) {
                            }
                        }

                        // Now update the progress.  Note it is important that
                        // we touch the progress bar with the lock held, so it
                        // doesn't disappear on us.
                        mPosition++;
                        max = mProgressBar.getMax();
                        mProgressBar.setProgress(mPosition);
                    }

                    // Normally we would be doing some work, but put a kludge
                    // here to pretend like we are.
                    synchronized (this) {
                        try {
                            wait(50);
                        } catch (InterruptedException e) {
                        }
                    }
                }
            }
        };

        /**
         * Fragment initialization.  We way we want to be retained and
         * start our thread.
         */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Tell the framework to try to keep this fragment around
            // during a configuration change.
            setRetainInstance(true);

            // Start up the worker thread.
            mThread.start();
        }

        /**
         * This is called when the Fragment's Activity is ready to go, after
         * its content view has been installed; it is called both after
         * the initial fragment creation and after the fragment is re-attached
         * to a new activity.
         */
        @Override
        public void onActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

            // Retrieve the progress bar from the target's view hierarchy.
            mProgressBar = (ProgressBar)getTargetFragment().getView().findViewById(
                    R.id.progress_horizontal);

            // We are ready for our thread to go.
            synchronized (mThread) {
                mReady = true;
                mThread.notify();
            }
        }

        /**
         * This is called when the fragment is going away.  It is NOT called
         * when the fragment is being propagated between activity instances.
         */
        @Override
        public void onDestroy() {
            // Make the thread go away.
            synchronized (mThread) {
                mReady = false;
                mQuiting = true;
                mThread.notify();
            }

            super.onDestroy();
        }

        /**
         * This is called right before the fragment is detached from its
         * current activity instance.
         */
        @Override
        public void onDetach() {
            // This fragment is being detached from its activity.  We need
            // to make sure its thread is not going to touch any activity
            // state after returning from this function.
            synchronized (mThread) {
                mProgressBar = null;
                mReady = false;
                mThread.notify();
            }

            super.onDetach();
        }

        /**
         * API for our UI to restart the progress thread.
         */
        public void restart() {
            synchronized (mThread) {
                mPosition = 0;
                mThread.notify();
            }
        }
    }
}

这篇关于进一步了解setRetainInstance(true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆