其他分享
首页 > 其他分享> > android-在TabHost内部未调用onActivityResult()

android-在TabHost内部未调用onActivityResult()

作者:互联网

我有两个活动,一个是选择器活动,另一个是选择列表.单击选择后,数据将返回到选择器活动.

这两个活动是活动组的一部分,活动组是TabHost内的TabSpec.

当选择器活动是启动活动时,一切正常.当ActivityGroup是启动活动时,它也可以正常工作.当TabHost是启动活动时,它不起作用(但没有错误).我需要做一些Intent标志或getParent()来使它起作用吗?

public class MyActivity extends Activity {
    static final int MY_REQUEST = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_picker);
        Button button = (Button) findViewById(R.id.my_button);
        button.setOnClickListener(buttonOnClickListener);
    }

    private OnClickListener buttonOnClickListener = new OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MyActivity.this, SubActivity.class);
            startActivityForResult(inent, MY_REQUEST);
        }
    };

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == MY_REQUEST) {
            if (resultCode == RESULT_OK) {
                String item = data.getStringExtra("item");
                //do something
            }
        }
        if (resultCode == RESULT_CANCELED) {

        }
    }
}


public class SubActivity extends Activity {
    private ArrayAdapter<String> adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_sublayout);
        ListView listView = (ListView) findViewById(R.id.list);
        String[] items = {"red", "blue", "black", "green"};
        adapter = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, items);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(listOnItemClickListener);
    }

    private OnItemClickListener listOnItemClickListener = new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent();
            String item = adapter.getItem(position);
            intent.putExtra("item", item);
            setResult(RESULT_OK, intent);
            finish();
        }
    };
}

用于在选项卡内启动活动的代码是相当标准的,但这里是:

public class TabsActivity extends TabActivity {

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

        TabHost tabHost = getTabHost();

        Intent intent= new Intent().setClass(this, MyActivityGroup.class);
        TabSpec tabSpecMyActivity = tabHost
            .newTabSpec(getString(R.string.my_title))
            .setIndicator(getString(R.string.my_title))
            .setContent(intent);

        tabHost.addTab(tabSpecMyActivity );

        tabHost.setCurrentTab(0);
    }
}

解决方法:

您是否考虑过在转到其他活动时不在onItemClick()方法内调用finish()方法,因为它可以终止该活动,然后再返回该活动,它将再次从onCreate()开始.

标签:activitygroup,android-tabactivity,android
来源: https://codeday.me/bug/20191031/1977987.html