Search code examples
androidandroid-fragmentsandroid-tabs

Android pass databundle from activity to fragments (tabs)


I've been trying to get an id from a listview-onclicklistener to three tabbed fragments. The user firstly clicks in myTicketsFragmentand then goes to the detail page which contains 3 swipeable tabs. These views are 'hosted' by one individual activity named TicketActivity. So currently I've succesfully passed data from the fragment to TicketActivity but I cannot go further than that. Been searching for 2 hours now and still no results..

Here's my code:

myTicketsFragment: passing the data in setOnItemClickListener to tab activity

public void onItemClick(AdapterView<?> parentView,
                        View childView, int position, long id) {

                    Bundle bundle = new Bundle();
                    bundle.putInt("ticketId", myTickets.get(position).getId());
                    Intent ticketDetail = new Intent(getActivity(), TicketActivity.class);
                    ticketDetail.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    ticketDetail.putExtras(bundle);
                    startActivity(ticketDetail);
}

TicketActivity: receiving data and passing it through to the 3 tabs

private ViewPager viewPager;
private TicketTabsPagerAdapter mAdapter;
private ActionBar actionBar;
// Tab titles
private String[] tabs = { "Info", "Intern", "Extern" };

public TicketInfoFragment ticketInfoFragment;

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

    // Receive data
    Bundle bundle = getIntent().getExtras();
    int ticketId = bundle.getInt("ticketId");

            // Pass data to fragments
            // ...

    // Initilization
    viewPager = (ViewPager) findViewById(R.id.pager);
    actionBar = getActionBar();
    mAdapter = new TicketTabsPagerAdapter(getSupportFragmentManager());

    viewPager.setAdapter(mAdapter);
    actionBar.setHomeButtonEnabled(true);
    actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

    // Adding Tabs
    for (String tab_name : tabs) {
        actionBar.addTab(actionBar.newTab().setText(tab_name)
                .setTabListener(this));
    }

    /**
     * on swiping the viewpager make respective tab selected
     * */
    viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

        @Override
        public void onPageSelected(int position) {
            // on changing the page
            // make respected tab selected
            actionBar.setSelectedNavigationItem(position);
        }

        @Override
        public void onPageScrolled(int arg0, float arg1, int arg2) {
        }

        @Override
        public void onPageScrollStateChanged(int arg0) {
        }
    });
}

Example of a tab fragment

public class TicketInfoFragment extends Fragment {

TicketFull ticket = new TicketFull();
private DatabaseHelper db;
int ticketId;
String androidId;
String authCode;
String platform_url;
int uId;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    db = new DatabaseHelper(getActivity());
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_ticket_info, container, false);

    return rootView;
}
}

I would be pleased if anyone could help me out

Thanks in advance


Solution

  • 2 quick ways:

    1. use an static method in your activity to retrieve current ticket id

    2. Design and implement an interface and register the fragments as listeners from the activity

    First option, In your activity:

        private static int ticketId;
    
        public static int getCurrentTicketId(){
             return ticketId;
        }
    

    and in your fragment you can do:

    TickerActivity.getCurrentTicketId();
    

    Second option, Use an interface:

    public interface TicketListener{
        public void onTicketChanged(int newTicket);
    }
    

    and in your activity add:

    public List<TicketListener> listeners = new ArrayList<TicketListener>();
    
    public void addListener(TicketListener listener){
        listeners.add(listener);
    }
    

    and register every fragment as a new listener

    YourFragment frag = new YourFragment();
    addListener(frag);
    

    and finally when you want to notify the key to the listeners iterate over the list:

    for(TicketListener listener : listeners){
        listener.onTicketChanged(ticket);
    }