user1881440
user1881440

Reputation:

Passing of value from one activity to another in android

Hi i have developed an app which has a text box and a search button when i enter a number in the text box and click on the search button it needs to pass the value entered to the next activity where it uses that value to get the value from a database.

I am using the following code to pass the value.

search_button.setClickable(true);
      search_button.setOnClickListener(new View.OnClickListener() {

      @Override
      public void onClick(View v) {
        // TODO Auto-generated method stub
        String outlet_no = outlet_id.getText().toString();
        System.out.println(outlet_no);
        if(!outlet_no.isEmpty()){
        @SuppressWarnings("deprecation")
        SharedPreferences myPrefs = getApplicationContext().getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
        SharedPreferences.Editor prefsEditor = myPrefs.edit();

        prefsEditor.putString("outlet_id", outlet_no);
        prefsEditor.commit();

        Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);       
        startActivity(myIntent);
        HomeActivity.this.startActivity(myIntent);
        }
        else{
          Toast.makeText(getApplicationContext(), "Please enter an outlet id", Toast.LENGTH_SHORT);
        }  
      }
    });

My problem is that it doesnot pass the value. Can any one help me with this and also can any1 explain me the above code as i didnot write it and i am finding it difficult to understand it.

StoreActivity side

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.store);      
        myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);             
        mOutletID = myPrefs.getString("outlet_id", "0");
        mOutletDetails = myPrefs.getString("outlet_details","{}");
        Log.v("outlet_details",myPrefs.getString("outlet_details","{}"));
        if(mOutletDetails != "{}"){
            setOutletData(mOutletDetails);
        }
        else{
            executeAjaxRequest();
        }
    }    

Upvotes: 5

Views: 18794

Answers (7)

Paresh Mayani
Paresh Mayani

Reputation: 128448

Mistakes you made:

1) Why are you trying to start activity twice?

startActivity(myIntent);   // this is OK
HomeActivity.this.startActivity(myIntent);

2) To pass entered text from one activity to another, you can use putExtra() method of Intent, for example:

myIntent.putExtra("SearchText", outlet_no);

So your complete code would be:

 Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);   
 myIntent.putExtra("SearchText", outlet_no);    
 startActivity(myIntent);

3) To receive value which you have passed from first activity:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String outlet_no= bundle.getString("SearchText");

4) SharedPreferences:

FYI, SharedPreference is used to save small amount of data which can be reference and used later across application.

You should refer below links:

  1. SharedPreferences
  2. Intent
  3. Intent and Intent Filters

Upvotes: 9

Mayur Chudasama
Mayur Chudasama

Reputation: 474

You can also do this with Intents.

Say, we have 2 activities: SourceActivity & DestinationActivity and we want to pass a value of SourceActivity to DestinationActivity

in your Button OnClickListener:

String str_outlet_no = outlet_no.getText().toString();
Intent intent = new Intent(SourceActivity.this, DestinationActivity.class);
intent.putExtra("outlet_no_key", outlet_no);
startActivity(intent);

and on the other side in DestinationActivity:

Intent intent = getIntent();
String desi = intent.getStringExtra("outlet_no");

and search desi from database

Upvotes: 0

Siddharth Srivastava
Siddharth Srivastava

Reputation: 1069

In your onClick method, have the following code:

    String outlet_no = outlet_id.getText().toString();
    if(!outlet_no.isEmpty()){
        Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);  
        myIntent.putExtra("outlet_id",outlet_no);
        startActivity(myIntent);
    }
    else{
        Toast.makeText(getApplicationContext(), "Please enter an outlet id", Toast.LENGTH_SHORT);
    }  

Now in the StoreActivity, in the onCreate() method have something like this:

String outlet_no = getIntent().getStringExtra("outlet_id");  
if (outlet_no != null) //Just a defensive check
   //Start your processing

Upvotes: 2

Naveen R
Naveen R

Reputation: 55

Activity A

myIntent.putExtra(var, value); // Putting the value
startActivity(myIntent);

Activity B

String value = getIntent().getStringExtra(var); // Getting the value

Upvotes: -1

Rahul Gokani
Rahul Gokani

Reputation: 1708

You can simply write it as

Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);
      myIntent.putExtra("outlet_no",outlet_no);
        startActivity(myIntent);  

Let me know if this worked or not.

Upvotes: -1

Rahul
Rahul

Reputation: 45080

You can try something like this:-

Activity A

myIntent.putExtra(var, value); // Putting the value
startActivity(myIntent);

Activity B

String value = getIntent().getStringExtra(var); // Getting the value

Upvotes: 0

Archie.bpgc
Archie.bpgc

Reputation: 24012

Intent myIntent = new Intent(HomeActivity.this, StoreActivity.class);
myIntent.putExtra("searchString", outlet_no);       
startActivity(myIntent);

This will send the searchString to the StoreActivity

and in StoreActivity you can retrieve the value using:

String s = getIntent.getStringExtra("searchString");

Upvotes: 3

Related Questions