3

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);
    }
}
Joe
  • 14,039
  • 2
  • 39
  • 49
Jason Christa
  • 12,150
  • 14
  • 58
  • 85
  • Why aren't you using actionbar with fragments instead? Didn't they deprecate tab host and tabs activity? – meh Oct 24 '12 at 20:57
  • @user1597833 because the is no first-party support for the actionbar in Android 2.3 – Jason Christa Oct 24 '12 at 21:06
  • well I still wouldn't use deprecated classes in my applications, does your code works on ics and later versions ? or do you write completely different code for different android versions ? – meh Oct 24 '12 at 21:21
  • @user1597833 Android is forwards compatible, which is why I am targeting 2.3 – Jason Christa Oct 24 '12 at 21:54
  • There is an `ActionBar` compatibility sample app. It can be obtained by downloading the samples for the SDK. Will you please post the code for the list of choices as well? – Thomas Oct 30 '12 at 22:14
  • @Thomas if you scroll down in the code a little it contains the SubActivity with the choices (red, blue, black, green in this case). – Jason Christa Oct 30 '12 at 22:56
  • @JasonChrista Completely missed that for some reason ;). Does this comment help you by any chance: http://stackoverflow.com/questions/7872962/android-onactivityresult-doesnt-work-on-tabhost?rq=1#comment9606236_7872962 – Thomas Oct 31 '12 at 17:44
  • Have you tried using a BroadcastReceiver and broadcast an intent when finishing the activity? – Adrian C. Oct 31 '12 at 19:16
  • Can you add the codes where you launch the myactivity? – Ron Nov 01 '12 at 08:01
  • Try calling SubActivity.this.setResult.. – Ron Nov 01 '12 at 08:03
  • @userSeven7s I did try SubActivity.this.setResult... but it didn't work. I also had onActivityResult methods in my ActivityGroup and TabActivity but they never receive any results either. – Jason Christa Nov 01 '12 at 13:28
  • Could you also show how you instantiated MyActivity and SubActivity from within MyActivityGroup? – Joe Nov 01 '12 at 20:29
  • Have you tried with `addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)` for starting the activity via tabhost in tabhost.add? – Vrashabh Irde Nov 03 '12 at 16:18
  • Also do these describe something similar to your problem ? http://stackoverflow.com/questions/9819848/onactivityforresult-is-not-called-in-the-activity-in-tabs and http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity. – Vrashabh Irde Nov 03 '12 at 16:27
  • After digging deeper maybe you should set `Intent.FLAG_ACTIVITY_FORWARD_RESULT` – Vrashabh Irde Nov 03 '12 at 16:31
  • @Slartibartfast I have tried both CLEAR_TOP and FORWARD_RESULT. – Jason Christa Nov 03 '12 at 23:27
  • How about creating a transparent activity on top that does nothing but sit on top of the tabhost.Activity A calls Activity B via normal intent. Activity B has no xml and runs onCreate like this public `void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //setContentView(R.layout.dialogpopper); Intent intent = new Intent(Intent.ACTION_PICK); intent.setType("image/*"); startActivityForResult(intent, 0); }//end onCreate` and when Activity C is finished it calls the onActivityResult of Activity B. Basically a hack from this link ? http://goo.gl/vB3aY – Vrashabh Irde Nov 04 '12 at 01:40

4 Answers4

3

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().

Yogesh Somani
  • 2,624
  • 3
  • 21
  • 34
  • 3
    I don't see how this answer will make the onActivityResult() become called under the TabHost scenario as described by the question. Can any of the upvoters explain? – Joe Nov 10 '12 at 02:57
1

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.

Joe
  • 14,039
  • 2
  • 39
  • 49
0

You need to start a startChildActivityForResult with the given parent your tabhost, this works in my application

sample:

Intent intent = new Intent(getParent(), childActivity.class);
TabGroupActivity parentActivity = (TabGroupActivity)getParent();
parentActivity.startChildActivityForResult("childActivity", intent, RESULT_OK);
Greezer
  • 515
  • 2
  • 4
  • 18
0

I had same issue like you when I was using the startActivityForResult method with the tab and ActivityGroup.

Actually problem is that when you start the activity from the ActivityGroup then you will not receive the ActivityResult within the calling activity instead you will get the result in the parent activity ie. ActivityGroup.

Try to override the onActivityResult method of the ActivityGroup and print the result there you will get the response there.

Dharmendra
  • 33,296
  • 22
  • 86
  • 129