Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult() not called inside a TabHost

I have two Activities, one is a picker activity and the other is a list of choices. Once a choice is clicked, data is returned to the picker activity.

These two activities are part of an activity group and the activity group is a TabSpec inside a TabHost.

Everything works fine when the picker Activity is the launch activity. It also works fine when the ActivityGroup is the launch activity. It does not work (but no error) when the TabHost is the launch activity. Is there some Intent flag or getParent() I need to be doing to get this to work?

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();
        }
    };
}

The code for launching the activity inside a tab is fairly standard but here it is:

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);
    }
}
like image 751
Jason Christa Avatar asked Mar 08 '26 16:03

Jason Christa


2 Answers

Have you considered not calling finish() method inside onItemClick() method while going to other activity because it can terminate this activity and then if we come back on this activity, it will start again from onCreate().

like image 95
Yogesh Somani Avatar answered Mar 10 '26 07:03

Yogesh Somani


I asked about how you instantiated MyActivity and SubActivity from within MyActivityGroup in the comment above because I don't think you really needed to use MyActivityGroup when you are running your TabsActivity. So in TabsActivity.onCreate(), instead of:

    Intent intent = new Intent().setClass(this, MyActivityGroup.class);

You can just do:

    Intent intent = new Intent().setClass(this, MyActivity.class);

With the above change, your MyActivity.onActivityResult() should be called as expected.

If you really need to have MyActivityGroup inside your TabsActivity, let me know and I will update this answer with additional modifications (in TabsActivity.onActivityResult() and MyActivityGroup.startActivityFromChild()) to make it work.

like image 33
Joe Avatar answered Mar 10 '26 05:03

Joe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!