0

I have a MainActivity.class which extends from Activity . I created a tab like structure . on clicking any of the image . new fragment is displayed. My first fragment is Article. it has a AsyncTask associate with it.

when i go to another fragment and comeback to it .asyncTask call again. And the whole listView is recreated . I simply want to disable AsyncTask for calling again and save the previous state of the fragment.

I have addToBackStack(null); while adding fragments.

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

where Article is a fragment and container is the frameLayout in the mainActivity.xml

I want to show the fragment where i left off. Like we do in Activities by setting LaunchMode+"Single" or something . I am not clearing backStack.

I simply need to switch between Activities like we do in TabActivity. in TabActivity if we want to repeat the AsyncTask we have to specify it in onResume(). otherwise it will not called

Article Fragment

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();


    }
}

and practicing.xml is

<?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>
Zar E Ahmer
  • 33,936
  • 20
  • 234
  • 300

2 Answers2

0

Try adding your fragment instead of replacing.

Nauman Afzaal
  • 1,046
  • 12
  • 20
0

What addToBackStack() does is: it adds the fragment transaction to its stack, so it knows that when you press back, it should show your first fragment. So t does exactly that, and shows your first fragment again and the fragment's onCreatView() is called again. Here in your onCreateView, all initialization occurs and the AsyncTask is called. This is why it happens everytime you change between fragments.

The solution I implemented for this problem is:

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

So you should change:

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;
    }
}

to:

  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;
    }
}

Here, we move View V outside and make it a class variable. So if it is the first time the fragment is called, it is null and the initialization occurs, otherwise it goes to else black. Else block is required because onCreateView() adds whatever it returns as a child of the view's parent, so since V is already there, we remove it and onCreateView automatically adds it again.

Kartik_Koro
  • 1,277
  • 1
  • 11
  • 23