Hi

I have created an Activity from the template of a Blank Activity (
http://developer.android.com/tools/projects/templates.html#blank-activity) 
with *Navigation Type: Tabs* or *Tabs + Swipe*


My code is below. I cannot find a way to remove a fragment. I can delete a 
tab from the pager at the Top but the fragment below is not removed. Please 
can you tell me what I am doing wrong? Any help would be appreciated.

Thanks in advance.

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;

public class MainActivity extends FragmentActivity {
    private static final String TAG = "Tag";

    SectionsPagerAdapter mSectionsPagerAdapter;
    Context context;

    ArrayList<String> titles;
    ArrayList<Fragment> frags;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getApplicationContext();

        //========================
        titles = new ArrayList<String>();
        frags = new ArrayList<Fragment>();
        titles.add("One");
        titles.add("Two");
        titles.add("Three");
        titles.add("Four");
        createFrags();
        //=========================

        setContentView(R.layout.activity_main);

        // Create the adapter that will return a fragment for each of the 
three
        // primary sections of the app.

        mSectionsPagerAdapter = new 
SectionsPagerAdapter(getSupportFragmentManager());
       // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    public void createFrags()
    {
        for (int i = 0; i < titles.size(); i++)
        {
        Fragment fragment = genFrag(i+"");
         frags.add(fragment);
        }
    }

    public Fragment genFrag(String i)
    {
        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putString(DummySectionFragment.ARG_SECTION_NUMBER, i);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onDestroy()
    {
        super.onDestroy();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is 
present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG,"onOptionsItemSelected");
        // Handle item selection
        switch (item.getItemId()) {
            case R.id.action_settings:
                Log.d(TAG,"Settings menu item pressed");
                Toast.makeText(this,"Settings",Toast.LENGTH_LONG);

                Intent i = new Intent(getApplicationContext(), 
SettingsActivity.class);
                startActivity(i);
                return true;
            case R.id.action_sendmsg:
                Log.d(TAG,"Send Message Option Pressed");
                // Start the service, keeping the device awake while it is 
launching.
                Intent intent =  new Intent(this, 
SendMsgIntentService.class);
                startService(intent);
                Log.d(TAG,"Service Should be started?");
                return true;
            case R.id.action_add:
                Log.d(TAG,"Add menu item pressed");
                addTab("New");
                return true;
            case R.id.action_delete:
                Log.d(TAG,"delete menu item pressed");
                int a = mViewPager.getCurrentItem();
                removeTab(a);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }

    public void addTab(String name)
    {
        Log.i(TAG,"Add Tab");
        titles.add("New");
        Fragment f = genFrag(name);
        frags.add(f);
        mSectionsPagerAdapter.notifyDataSetChanged();

        /*
        int position = mSectionsPagerAdapter.getCount()+1;

        Fragment fragment = new DummySectionFragment();
        Bundle args = new Bundle();
        args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position);
        fragment.setArguments(args);

        mSectionsPagerAdapter.instantiateItem(mViewPager, position);
        mSectionsPagerAdapter.notifyAll();
        */
    }

    public void removeTab(int position)
    {
        Log.i(TAG,"Remove Tab");
        if(titles.size()==1)
        {
            Toast.makeText(context, "You need to have at least one 
page.",Toast.LENGTH_LONG).show();
        }else{
        titles.remove(position);
        frags.remove(position);

        mSectionsPagerAdapter.notifyDataSetChanged();
        }
     /*
        mSectionsPagerAdapter.destroyItem(mViewPager, position, 
mSectionsPagerAdapter.getItem(position));
        mSectionsPagerAdapter.notifyDataSetChanged();
     */
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding 
to
     * one of the sections/tabs/pages.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }


        @Override
        public Fragment getItem(int position) {
            // getItem is called to instantiate the fragment for the given 
page.
            // Return a DummySectionFragment (defined as a static inner 
class
            // below) with the page number as its lone argument.
            return frags.get(position);
        }

        @Override
        public int getCount() {
            // Show 3 total pages.
            return frags.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return titles.get(position);
        }
    }

    /**
     * A dummy fragment representing a section of the app, but that simply
     * displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        /**
         * The fragment argument representing the section number for this
         * fragment.
         */
        public static final String ARG_SECTION_NUMBER = "section_number";

        public DummySectionFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup 
container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main_dummy, 
container, false);
            TextView dummyTextView = (TextView) 
rootView.findViewById(R.id.section_label);
            
dummyTextView.setText(getArguments().getString(ARG_SECTION_NUMBER));
            return rootView;
        }
    }

}




>

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to