使用TabGroupActivity时出现startChildActivity问题

时间:2014-01-03 06:30:37

标签: android

Fallow是家庭课。我将调用Pick_contact意图。单击ContactNumber视图时,将显示设备联系人列表。选择联系的结果是TabGroupActivity。

public class Home extends Activity{
    private static int PICK_CONTACT= 1;
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }

public void ContactNumber(View v)
    {
       Intent intent = new  Intent(Intent.ACTION_PICK,ContactsContract.Contacts.CONTENT_URI);
       parentActivity = (TabGroupActivity)getParent();
       parentActivity.startActivityForResult(intent, PICK_CONTACT);
    }

}

我的TabGroupActivity代码如下所示。

public class TabGroupActivity extends ActivityGroup {

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

if(requestCode == PICK_CONTACT)
        {
             if (resultCode == Activity.RESULT_OK) {

                 Uri contactData = data.getData();
                 Cursor c =  managedQuery(contactData, null, null, null, null);
                 String cNumber="";
                 if (c.moveToFirst()) {


                     String id =c.getString(c.getColumnIndexOrThrow(ContactsContract.Contacts._ID));

                     String hasPhone =c.getString(c.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER));

                       if (hasPhone.equalsIgnoreCase("1")) {
                      Cursor phones = getContentResolver().query( 
                                   ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
                                   ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = "+ id, 
                                   null, null);
                         phones.moveToFirst();
                          cNumber = phones.getString(phones.getColumnIndex("data1"));
                         System.out.println("number is:"+cNumber);
                       }
                     String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
                    QRCodeStaticData.qr_contents=name;

                 }

               }
        }
}

}

在上面的代码中,当用户从列表中选择联系人时,我想打开其他子活动。但如果用户没有接听联系并取消联系,用户将离开家庭活动。在选择联系人后,我没有得到如何在TabGroupActivity中调用子活动。我使用下面的代码来调用子活动。

Intent intent = new Intent(getParent(), CreateQRCode.class);
TabGroupActivity parentActivity = (TabGroupActivity) getParent();
parentActivity.startChildActivity("CreateQRCode", intent);

但它在TabGroupActivity的onActivityResult中不起作用。

1 个答案:

答案 0 :(得分:2)

试试这个......对于迟到的答案抱歉

TabGroupActivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tabHost = (TabHost) findViewById(android.R.id.tabhost);
    // Adding the Activities to the tab view
    // Blah Blah
}

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {
        System.out.println("Success");
        if (requestCode == PICK_CONTACT) {
            //Here you can launch the Child Activity according to the index
            //Here CreateQRCode Activity index is 1 in the TabView
            tabHost.setCurrentTab(1);
        }
    } else {
        System.out.println("Fail");
    }
}

HomeActivity

public void ContactNumber(View view) {
    Intent intent = new Intent(Intent.ACTION_PICK,
            ContactsContract.Contacts.CONTENT_URI);
    startActivityForResult(intent, PICK_CONTACT);
}