Code example for FragmentActivity

Methods: managedQuery, onRestart

0
    } 
 
    /** @see android.app.Activity#onRestart() */ 
    @Override 
    public void onRestart() { 
        super.onRestart(); 
        setAdapter(contacts);
    } 
 
    void launchDetails(int pos, boolean useFrag) {
        Cursor cursor = (Cursor) contacts.getAdapter().getItem(pos);
 
        String id = cursor.getString(
            cursor.getColumnIndex(BaseColumns._ID));
        String name = cursor.getString(
                cursor.getColumnIndex(Contacts.DISPLAY_NAME));
 
        if (useFrag) { stackFragment(id, name); }
        else { stackActivity(id, name); }
    } 
 
    private void installFragment() { 
        FragmentManager fragMgr = getSupportFragmentManager();
         
        if (null != fragMgr.findFragmentByTag(FRAG_TAG)) { return; }
 
        FragmentTransaction xact = fragMgr.beginTransaction();
        xact.add(
            R.id.contact_detail, 
            ContactDetailFragment.newInstance(null, null), 
            FRAG_TAG);
        xact.commit();
    } 
 
    private void stackFragment(String id, String name) {
        FragmentTransaction xact
            = getSupportFragmentManager().beginTransaction(); 
 
        xact.replace(
            R.id.contact_detail, 
            ContactDetailFragment.newInstance(id, name),
            FRAG_TAG);
 
        xact.addToBackStack(null);
        xact.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
 
        xact.commit();
    } 
 
    private void stackActivity(String id, String name) {
        Intent intent = new Intent();
        intent.setClass(this, ContactDetailActivity.class);
        intent.putExtra(ContactDetails.TAG_ID, id);
        intent.putExtra(ContactDetails.TAG_CONTACT, name);
        startActivity(intent);
    } 
 
    private void setAdapter(ListView view) {
        if (null == view) { return; }
        view.setAdapter(
            new ContactsCursorAdapter( 
                this,
                managedQuery( 
                    Contacts.CONTENT_URI,
                    CONTACTS_PROJECTION,
                    CONTACTS_FILTER,
                    null, 
                    CONTACTS_SORT)));