AsyncTask在片段之间切换时重新执行

时间:2014-07-16 07:25:05

标签: android android-asynctask fragment

我有一个MainActivity.class,它来自Activity。我创建了一个类似标签的结构。点击任何图像。显示新片段。我的第一个片段是Article。它与AsyncTask有关联。

当我转到另一个片段并重新回到它时.asyncTask再次调用。并重新创建整个listView。我只是想禁用AsyncTask再次调用并保存片段的先前状态。

我在添加片段时addToBackStack(null);

getFragmentManager().beginTransaction().replace(R.id.container, new Article()).addToBackStack(null).commit();

其中Article是fragment,而容器是mainActivity.xml中的frameLayout

我想要显示我离开的片段。就像我们在活动中通过设置LaunchMode +“Single”或其他东西一样。我没有清理backStack。

我只需要在TabActivity中切换活动。在TabActivity中,如果我们想重复AsyncTask,我们必须在onResume()中指定它。否则不会调用

文章片段

public class Article extends Fragment
{
    ListView listView;

    Activity context;

    ArrayList<HashMap<String,String>> art_list;

    ArticleAdapter adapter;

    String url="http://tabletennisdaily.co.uk/webservices/view_articles.php";

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

        context = getActivity();

        listView = (ListView) V.findViewById(R.id.art_listview);

        new ArticleTask(getActivity(), url, listView).execute(url);

        return V;
    }
}

MainActivity

public class MainActivity extends Activity //implements FragmentDelegate,FragmentManager.OnBackStackChangedListener
{
    public LinearLayout Tab1,Tab2,Tab3,Tab4;

    public ImageView img1,img2,img3,img4;

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

        init();
    }

    private void init()
    {
        Tab1 = (LinearLayout) findViewById(R.id.tab1);

        Tab2 = (LinearLayout) findViewById(R.id.tab2);

        Tab3 = (LinearLayout) findViewById(R.id.tab3);

        Tab4 = (LinearLayout) findViewById(R.id.tab4);

        img1 = (ImageView)findViewById(R.id.tab_img1);

        img2 = (ImageView)findViewById(R.id.tab_img2);

        img3 = (ImageView)findViewById(R.id.tab_img3);

        img4 = (ImageView)findViewById(R.id.tab_img4); 

        getFragmentManager().beginTransaction().replace(R.id.container, new Article()).addToBackStack(null).commit();   
    }

    public void selectFrag(View view) {

        Fragment fr = null;

        if (view == findViewById(R.id.tab1)) 
        {           
            img1.setBackgroundResource(R.drawable.articles_on);

            img2.setBackgroundResource(R.drawable.forum_off);

            img3.setBackgroundResource(R.drawable.video_off);

            img4.setBackgroundResource(R.drawable.profile_off);

            fr = new Article();
        } 
        else if(view == findViewById(R.id.tab2)) 
        {
            img1.setBackgroundResource(R.drawable.articles_off);

            img2.setBackgroundResource(R.drawable.forum_on);

            img3.setBackgroundResource(R.drawable.video_off);

            img4.setBackgroundResource(R.drawable.profile_off);

            fr = new Forum();
        }
        else if(view == findViewById(R.id.tab3))
        {
            img1.setBackgroundResource(R.drawable.articles_off);

            img2.setBackgroundResource(R.drawable.forum_off);

            img3.setBackgroundResource(R.drawable.video_on);

            img4.setBackgroundResource(R.drawable.profile_off);

            fr = new Medias();
        }
        else if(view == findViewById(R.id.tab4))
        {
            img1.setBackgroundResource(R.drawable.articles_off);

            img2.setBackgroundResource(R.drawable.forum_off);

            img3.setBackgroundResource(R.drawable.video_off);

            img4.setBackgroundResource(R.drawable.profile_on);

            fr = new Profile();
        }

        FragmentManager fm = getFragmentManager();

        FragmentTransaction fragmentTransaction = fm.beginTransaction();

        fragmentTransaction.replace(R.id.container, fr);

        fragmentTransaction.addToBackStack(null);//MainActivity.TAG);//.addToBackStack(null);

        fragmentTransaction.commit();


    }
}

practising.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/bottom" />

    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:baselineAligned="false"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >

        <LinearLayout
            android:id="@+id/tab1"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:onClick="selectFrag"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                android:id="@+id/tab_img1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/articles_on"
                android:padding="10dp"
                android:scaleType="center" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:onClick="selectFrag"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                 android:id="@+id/tab_img2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/forum_off"
                android:padding="10dp"
                android:scaleType="center" />

        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab3"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:onClick="selectFrag"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                 android:id="@+id/tab_img3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/video_off"
                android:padding="10dp"
                android:scaleType="fitXY" />


        </LinearLayout>

        <LinearLayout
            android:id="@+id/tab4"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:onClick="selectFrag"
            android:gravity="center"
            android:orientation="vertical" >

            <ImageView
                 android:id="@+id/tab_img4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="10dp"
                android:background="@drawable/profile_off"
                android:padding="10dp"
                android:scaleType="fitXY" />


        </LinearLayout>
    </LinearLayout>

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

尝试添加片段而不是替换。

答案 1 :(得分:0)

addToBackStack()的作用是:它将片段事务添加到其堆栈中,因此它知道当您按下时,它应该显示您的第一个片段。因此,t就是这样,并再次显示您的第一个片段,片段“onCreatView() 再次调用。在onCreateView中,所有初始化都会发生,并且AsyncTask被调用。这就是每次在片段之间进行更改时都会发生的原因。

我为此问题实施的解决方案是:

1) Keep view of fragment as a class variable.
2) Perform all actions in onCreateView() only if the view is null.

所以你应该改变:

public class Article extends Fragment
{
    ListView listView;

    Activity context;

    ArrayList<HashMap<String,String>> art_list;

    ArticleAdapter adapter;

    String url="http://tabletennisdaily.co.uk/webservices/view_articles.php";

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

        context = getActivity();

        listView = (ListView) V.findViewById(R.id.art_listview);

        new ArticleTask(getActivity(), url, listView).execute(url);

        return V;
    }
}

为:

  public class Article extends Fragment
{   
    ListView listView;

    Activity context;

    ArrayList<HashMap<String,String>> art_list;

    ArticleAdapter adapter;

    String url="http://tabletennisdaily.co.uk/webservices/view_articles.php";

    View V; //SET THE FRAGMENTS VIEW AS A CLASS VARIABLE HERE    

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState)
    {   //CHECK IF FRAGMENT IS INSTANTIATED BEFORE. IF IT IS NOT, CREATE NEW VIEW
        if(V == null){ 
            V = inflater.inflate(R.layout.article, container, false);

            context = getActivity();

            listView = (ListView) V.findViewById(R.id.art_listview);

            new ArticleTask(getActivity(), url, listView).execute(url);

        }
        else{ //IF ALREADY INSTANTIATED USE SAME OLD V 
            ((ViewGroup)V.getParent()).removeView(V);
        }

        return V;
    }
}

在这里,我们将View V移到外面并使其成为类变量。因此,如果它是第一次调用片段,则它为null并且初始化发生,否则它将变为其他黑色。其他块也是必需的,因为onCreateView()会添加它作为视图父级子项返回的内容,因此由于V已经存在,我们会将其删除,onCreateView会自动添加它。