29

I am trying to pass the data between Activities

I use intents to pass data between regular activities

consider the code below::

AndroidTabRestaurantDescSearchListView.java

public class AndroidTabRestaurantDescSearchListView extends TabActivity {

    // TabSpec Names
    private static final String INBOX_SPEC = "Rating";
    private static final String OUTBOX_SPEC = "Price";

    Button Photos;
    Button Filter;
    Button Search;

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

        TabHost tabHost = getTabHost();

        // Inbox Tab
        TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        Intent inboxIntent = new Intent(this, RatingDescriptionSearchActivity.class);
        inboxSpec.setIndicator(INBOX_SPEC);
        // Tab Content
        inboxSpec.setContent(inboxIntent);

        // Outbox Tab
        TabSpec PriceSpec = tabHost.newTabSpec(OUTBOX_SPEC);
        Intent PriceIntent = new Intent(this, PriceDescriptionSearchActivity.class);
        PriceSpec .setIndicator(OUTBOX_SPEC);
        PriceSpec.setContent(PriceIntent);


        // Adding all TabSpec to TabHost
        tabHost.addTab(inboxSpec); 
        tabHost.addTab(PriceSpec); 

        //Set the current value tab to default first tab
        tabHost.setCurrentTab(0);

    }

}

Suppose i send data from Someother activity called Activity-1 to AndroidTabRestaurantDescSearchListView as intents

Now how can i recieve the data in AndroidTabRestaurantDescSearchListView which i got from Activity-1 and then again pass it into RatingDescriptionSearchActivity

Pictoral representation is ::

enter image description here

{EDIT} -- If this is possible based on answers --- Ambiguity because AndroidTabRestaurantDescSearchListView is a tab activity

TabSpec inboxSpec = tabHost.newTabSpec(INBOX_SPEC);
        Intent inboxIntent = new Intent(this, RatingDescriptionActivity.class);
        intent.putExtra("keyName", value);
        inboxSpec.setIndicator(INBOX_SPEC);
        // Tab Content
        inboxSpec.setContent(inboxIntent);
Ratilal Chopda
  • 4,162
  • 4
  • 18
  • 31
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • see this will help you http://stackoverflow.com/questions/18649728/android-cannot-pass-intent-extras-though-alarmmanager/18649849#18649849 – Haresh Chhelana Oct 10 '13 at 04:22

7 Answers7

64

Pass the data from Activity-1 to AndroidTabRes.. as below:

At sending activity...

Intent intent = new Intent(current.this, AndroidTabRestaurantDescSearchListView.class);
intent.putExtra("keyName","value");
startActivity(intent);

At AndroidTabRes.. activity...

  String data = getIntent().getExtras().getString("keyName");

Thus you can have data at receiving activity from sending activity...

And in your AndroidTabRestaurantDescSearchListView class, do this:

String value= getIntent().getStringExtra("keyName");

Intent intent = new Intent(this, RatingDescriptionSearchActivity.class);
intent.putExtra("keyName", value);
startActivity(intent);

Then in your RatingDescriptionSearchActivity class, do this:

 String data= getIntent().getStringExtra("keyName");
Roozbeh Zabihollahi
  • 7,207
  • 45
  • 39
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
  • Please look at the updated question ..... my ambuiguity lies because one of them is tab activity ....... Am i following correct way based on your answer ..look at the edit – Devrath Oct 10 '13 at 04:44
  • Then what is your problem ? – GrIsHu Oct 10 '13 at 04:48
  • In the edit .... Have i mentioned proper way of passing data .... because in tabactivity ... we never use startActivity(--); so ... i am confused ! – Devrath Oct 10 '13 at 04:49
  • That doesn't make any difference you can pass and get the data as i have shown. – GrIsHu Oct 10 '13 at 04:58
  • what if the activity is already running, is there need to do `startActivity(i);` ? I mean, can I make *activity A* call *activity B*, and that returns data to *activity A* ? am I confused ? – Francisco Corrales Morales May 08 '14 at 23:25
7

Try this from your AndroidTabRestaurantDescSearchListView activity

Intent intent  = new Intent(this,RatingDescriptionSearchActivity.class );
intent.putExtras( getIntent().getExtras() );
startActivity( intent );  

And then from RatingDescriptionSearchActivity activity just call

getIntent().getStringExtra("key")
Jans
  • 11,064
  • 3
  • 37
  • 45
  • please look at the edited question .... my ambiguity is because middle activity is a tab activity – Devrath Oct 10 '13 at 04:48
  • 1
    The answer is still the same, you must send an `Intent` from first activity to `TabActivity` and when you want to fire third activity you must perform the answer setup. – Jans Oct 10 '13 at 04:58
5

You can use Bundle to get data :

Bundle extras = intent.getExtras();
String data = extras.getString("data"); // use your key 

And again you can opass this data to next activity :

 Intent intent = new Intent(this, next_Activity.class);
   intent.putExtra("data", data);
   startActivity(intent);
SweetWisher ツ
  • 7,296
  • 2
  • 30
  • 74
3

Simple.

Assuming that in your Activity-1, you did this:

String stringExtra = "Some string you want to pass";

Intent intent = new Intent(this, AndroidTabRestaurantDescSearchListView.class);

//include the string in your intent
intent.putExtra("string", stringExtra);

startActivity(intent);

And in your AndroidTabRestaurantDescSearchListView class, do this:

//fetch the string  from the intent
String extraFromAct1 = getIntent().getStringExtra("string");

Intent intent = new Intent(this, RatingDescriptionSearchActivity.class);

//attach same string and send it with the intent
intent.putExtra("string", extraFromAct1);
startActivity(intent);

Then in your RatingDescriptionSearchActivity class, do this:

String extraFromAct1 = getIntent().getStringExtra("string");
Razgriz
  • 7,179
  • 17
  • 78
  • 150
3

Main Activity

public class MainActivity extends Activity {

    EditText user, password;
    Button login;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        user = (EditText) findViewById(R.id.username_edit);
        password = (EditText) findViewById(R.id.edit_password);
        login = (Button) findViewById(R.id.btnSubmit);
        login.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,Second.class);

                String uservalue = user.getText().toString();
                String name_value = password.getText().toString();
                String password_value = password.getText().toString();

                intent.putExtra("username", uservalue);
                intent.putExtra("password", password_value);
                startActivity(intent);
            }
        });
    }
}

Second Activity in which you want to receive Data

public class Second extends Activity{

    EditText name, pass;
    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.second_activity);

        name = (EditText) findViewById(R.id.editText1);
        pass = (EditText) findViewById(R.id.editText2);

        String value = getIntent().getStringExtra("username");
        String pass_val = getIntent().getStringExtra("password");
        name.setText(value);
        pass.setText(pass_val);
    }
}
sHOLE
  • 343
  • 4
  • 15
Sachindra N. Pandey
  • 1,177
  • 17
  • 15
1

In FirstActivity:

Intent sendDataToSecondActivity = new Intent(FirstActivity.this, SecondActivity.class);
sendDataToSecondActivity.putExtra("USERNAME",userNameEditText.getText().toString());
sendDataToSecondActivity.putExtra("PASSWORD",passwordEditText.getText().toString());
startActivity(sendDataToSecondActivity);

In SecondActivity

In onCreate()

String userName = getIntent().getStringExtra("USERNAME");
String passWord = getIntent().getStringExtra("PASSWORD");
sHOLE
  • 343
  • 4
  • 15
Ramesh Bandari
  • 148
  • 2
  • 6
0

I like to use this clean code to pass one value only:

startActivity(new Intent(context, YourActivity.class).putExtra("key","value"));

This make more simple to write and understandable code.

Daniel Beltrami
  • 756
  • 9
  • 22