2

I am developing an app in which I am using multiple Activity Under Tab Activity .I am Using this Tutorial.

I want to get the Result from next Activity. How can i do it. I am not able to find it. I have read two or three Example such as this and this. But I am not able to find out how can I get the result. I also tried

    View view = getLocalActivityManager().startActivityForResult("Search", 
new Intent(this, WhatMenu.class).
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)).getDecorView();    
But it only Supports `startActivity()`

. Any Help will be Appreciable.
Thanks in Advance

Community
  • 1
  • 1
Nitin
  • 1,966
  • 4
  • 22
  • 53

3 Answers3

3

I have solved it via ViewFlipper Thanks every body for help.

Nitin
  • 1,966
  • 4
  • 22
  • 53
1

You need to pass request-code also for using startActivityForResult(). If you don't know what is it, just pass 0.
The syntax for startActivity() and startActivityForResult() is different.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
  • sir I have tried it but there is no method getLocalActivityManager().startActivityForResult(i,29); if I try it from group Activity it hides the tab which I do not want – Nitin Apr 11 '12 at 09:56
  • ahh.. there's no method for results in **LocalActivityManager**. May I know the purpose, so that we can think of any alternative. – 0xC0DED00D Apr 11 '12 at 10:02
  • Sir I simply want to get some data from next Activity to previous and notify the list; – Nitin Apr 11 '12 at 10:06
  • To get the data from the next Activity into previous Activity, you can pass values using sharedpreferances or using a static variable, though the later is not recommended. – 0xC0DED00D Apr 11 '12 at 10:10
  • But when I can notify because on resume() is also not working. – Nitin Apr 11 '12 at 10:13
  • One alternative would be to use a thread which checks if the values are updated in the sharedpreferance or not. You can start it after the startActivity(). Though there must be some clean approach to do it. I'll update, if I find one. – 0xC0DED00D Apr 11 '12 at 11:07
0

Activity 1
Create a class variable for reference.

private final int REQUEST_CODE = 0;

...
//Somewhere in your code you have to call startActivityForResult
Intent intent = new Intent(Activity1.this, Activity2.class);
startActivityForResult(intent);


Activity 2

Before ending Activity2 you have to set the result to OK and place the data that you want to bring back to Activity1 likeso

Intent data = new Intent();
data.putExtra("name", "Mark");
data.putExtra("number", 1);
data.putExtra("areYouHappy", true);

setResult(RESULT_OK, data);
finish(); //closes Activity2 and goes back to Activity1


Now go back to Activity1, you should override onActivityResult method and RETRIEVE the values from Activity2.
You do this by first checking if Activity2's result is OK, then check the reference REQUEST_CODE you passed. Since earlier we created private final int REQUEST_CODE = 0 then we check if requestCode is equal to the variable REQUEST_CODE. If it is then extract the data from Activity 2.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode==RESULT_OK) {
        if(requestCode==REQUEST_CODE) {
            if(data.getExtras()!=null) {
                String name = data.getStringExtra("name");
                int number = data.getIntExtra("number",0); //2nd parameter is the default value in case "number" does not exist 
                boolean areYouHappy = data.getBooleanExtra("areYouHappy", false); //2nd parameter is the default value in case "areYouHappy" does not exist
            }
        }
    }
}
Mark Pazon
  • 6,167
  • 2
  • 34
  • 50
  • Java don't have global variables, and using private and final, makes it less accessible, not global. Do you mean to say class variable or static variable? – 0xC0DED00D Apr 11 '12 at 11:28