3

my Class A calls a startActivityForResult: class A is activitygroup in the one tab of tabactivity

Intent intent = new Intent(this, ClassB.class);
startActivityForResult(intent, "STRING");

ClassB is a common activity ,not in the tabactivity,it is alone

Intent intent = this.getIntent();
intent.putExtra("SOMETHING", "EXTRAS");
this.setResult(RESULT_OK, intent);
finish();

but in Class A onactivityResult not receive anying,not excute.if i put classA out of the tabactivity,my app is ok. i have read

How to setResult() for a TabActivity which contains activity for tabs

How to return a result (startActivityForResult) from a TabHost Activity?

but not solve my problem,can somebody an give some advice ,code is more better,thank you

edit: in class A i add:

public class UpdateImageBroadcastReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {

            Log.i("temp","34353");


        }
    }



    /* (non-Javadoc)
     * @see android.app.Activity#onPause()
     */
    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        unregisterReceiver(updateImageBroadcastReceiver);

    }

    /* (non-Javadoc)
     * @see android.app.Activity#onResume()
     */
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

        updateImageFilter = new IntentFilter("ACTION_CHANGE_TAB");
        updateImageBroadcastReceiver = new UpdateImageBroadcastReceiver();
        registerReceiver(updateImageBroadcastReceiver, updateImageFilter);


    }

in class B:

   Intent intent = new Intent("ACTION_CHANGE_TAB");
                        intent.putExtra("path", f.getAbsolutePath());
                        sendBroadcast(intent);

                    //  FileListSelectActivity.this.setResult(RESULT_OK, intent);
                        finish();

but i cannot print: Log.i("temp","34353");

Community
  • 1
  • 1
pengwang
  • 19,536
  • 34
  • 119
  • 168

3 Answers3

1

Don't know if it is a the best method but currently I use a class that I call memory to pass objects between the activities.

public class Memory {
    public static Object objectToPass = null;
}
Roel Veldhuizen
  • 4,613
  • 8
  • 44
  • 78
  • thank you it give me someAdvice,but through i can get the value,but how to update ui,you know in the onactivityResult when receive resultok,it can update ui,i cannoy again startactivity class a – pengwang Aug 31 '11 at 11:31
1

Try using broadcast receivers. You can override onStop() in activity B, broadcast an event from activity B and listen in activity A.

superM
  • 8,605
  • 8
  • 42
  • 51
  • i will try ,but your code can update my ui when the activity b is over – pengwang Aug 31 '11 at 11:47
  • You can broadcast events whenever you like, onPause for example. There are no constraint. – superM Aug 31 '11 at 11:50
  • Intent intent = new Intent(this, ClassB.class); startActivityForResult(intent, "STRING"); – pengwang Aug 31 '11 at 13:17
  • i have using broadcast receivers,but cannot get anying,you can see my new edit – pengwang Sep 01 '11 at 02:37
  • thank you,i add updateImageFilter = new IntentFilter("ACTION_CHANGE_TAB"); updateImageBroadcastReceiver = new UpdateImageBroadcastReceiver(); registerReceiver(updateImageBroadcastReceiver, updateImageFilter); also add to the oncreat it is ok – pengwang Sep 01 '11 at 03:02
1

I can't found problem exactly without source code, but it could be one of next situation

1) Root activity for tabhost doesn't receive onActivityResult. Then you should call overriden onActivityResult in Activity inside TabHost

2) Another situation: Activity inside tabhost doesn't receive onActivityResult. Only focused activity will receive any actions inside ActivityGroup. You should place focus to some view inside it, then you will receive actions.

Henry Pootle
  • 1,196
  • 1
  • 11
  • 30
  • i found that the focuse is on the tabactivity,how to let view of class A to get focuse? – pengwang Aug 31 '11 at 11:58
  • you may set android:focusableInTouchMode="true" android:focusable="true" for root activity for example. But you should requestFocus manual during tab switching – Henry Pootle Aug 31 '11 at 12:38
  • i used Intent intent = new Intent(this, ClassB.class);linearlayout.requesFocus(); startActivityForResult(intent, "STRING"); the linearlayout is part of Class a,but no useful – pengwang Aug 31 '11 at 13:19
  • This trick works in my app, but I set focus just after switching. As I know requestFocus doesn't set it immediately. At first try to place editbox int your activity and put focus there just before calling activity ClassB. Only for test. – Henry Pootle Aug 31 '11 at 13:30
  • Did you add id to focusable linear layout? – Henry Pootle Aug 31 '11 at 20:50