onActivityResult()不会在TabActivity中调用

时间:2013-01-02 08:43:24

标签: android android-tabhost

我创建了TabActivity的子类,它包含四个TabHost标签。

我只想从我的应用中选择联系人,为此我使用以下代码

private final int PICK = 2;
Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI); 
    parentActivity.startActivityForResult(intentContact, PICK);

Android Contact Picker屏幕调用成功,当我点击任何联系人时,它会将我重定向到我的应用程序的最后一个屏幕,我调用它但onActivityResult方法没有被调用。

在这方面,我需要你的帮助。如果我忘记提及任何事情,请告诉我。

3 个答案:

答案 0 :(得分:1)

如你所愿,这里有一些代码。

public class MyActivity extends TabActivity
{
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
            super.onCreate(savedInstanceState);

            // some other stuff

            TabHost.TabSpec firstTab = tabHost.newTabSpec("firstTab");

            firstTab.setContent(new Intent(this, FirstTabActivity.class)); // your class with content picker

            tabHost.addTab(firstTab);
    }
}

这是另一个

public class FirstTabActivity extends Activity
{
    private static final int PICK_REQUEST_CODE = 2;

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

            // some stuff

            Button pickContactButton = findViewById(R.id.btn_pick_contact);

            pickContactButton.setOnClickListener(new View.OnClickListener()
            {
                    @Override
                    public void onClick(View view)
                    {
                            Intent intentContact = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                            startActivityForResult(intentContact, PICK_REQUEST_CODE);
                    }
            });
    }

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

            if(PICK_REQUEST_CODE == requestCode && resultCode == RESULT_OK)
            {
                    // do some stuff
            }
    }
}

答案 1 :(得分:0)

TabActivity有几个奇怪的行为,这可能是其中之一,你可能会解决它使其工作,但我肯定会建议你开始删除它,它已经被API级别13弃用。阅读更多相关信息:(https://developer.android.com/reference/android/app/TabActivity.html

答案 2 :(得分:0)

其他做法是......

1.On button click(which you want to open intent) open another activity.
2.On another activity's onCreate() open that intent.
3.onActivityResult set static data which is on previous activity and call finish().
4.This way no one ever know that you opened such a activity and you ll get your data.
:)