Passing value from one activity to another in android

Hi, I developed an application that has a text box and a search button, when I enter a number in the text box and click the search button to transfer the value specified in the next step, where it uses this value to get the value from the database.

I use the following code to pass a 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 does not pass value. Can someone help me with this, and also can any1 explain the code above to me since I haven't written it, and it's hard for me 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();
        }
    }    
+5
source share
8 answers

The mistakes you made:

1) Why are you trying to start activity twice?

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

2) , putExtra() Intent, :

myIntent.putExtra("SearchText", outlet_no);

, :

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

3) , :

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

4) SharedPreferences:

FYI, SharedPreference , .

:

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

searchString StoreActivity

StoreActivity :

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

onClick :

    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);
    }  

StoreActivity onCreate() - :

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

.

, 2 : SourceActivity DestinationActivity SourceActivity DestinationActivity

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);

DestinationActivity:

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

desi

+1

- : -

A

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

B

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

- signout , :

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("EXTRA_SESSION_ID", sessionId);
startActivity(intent)

docs Intents ( "" ).

0

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

, .

-1
source

Activity A

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

Activity B

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

All Articles