-2

My app has 2 activites.

  1. The first activity is just a simple form where a user enters course information(class title, professor..etc.) the first activity passes the data which is supposed to be stored in a list.

  2. In the second activity. The problem is that only the first course gets stored in the list, after the first time nothing new gets added to the second activity.

How can i do this ?

Sulthan Allaudeen
  • 10,822
  • 12
  • 44
  • 59
user3458541
  • 104
  • 4
  • How about you post some relevant code? Show us what you tried so far. – Xaver Kapeller Mar 25 '14 at 11:06
  • please post your code that helps alot to give u as an appropriate ans thanks :) – Farhan Shah Mar 25 '14 at 11:11
  • here is a good example about how to switch between activities and send data between them : http://www.android-ios-tutorials.com/117/how-to-switch-between-different-activities-in-android/ – Houcine Mar 25 '14 at 12:12

6 Answers6

5

In your first activity :

Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
                            intent.putExtra("login", jObj.getString(KEY_LOGIN));
                            intent.putExtra("mdp", jObj.getString(KEY_MDP));
                            intent.putExtra("prenom", jObj.getString(KEY_PRENOM));
                            intent.putExtra("nom", jObj.getString(KEY_NOM));
                            intent.putExtra("mail", jObj.getString(KEY_MAIL));
                            intent.putExtra("tel", jObj.getString(KEY_TEL));
                            startActivity(intent);

In your second activity :

Intent intent = getIntent();

          if (intent != null) {
              login = intent.getStringExtra("login");
              mdp = intent.getStringExtra("mdp");

              items.add(intent.getStringExtra("login"));
              items.add(intent.getStringExtra("prenom"));
              items.add(intent.getStringExtra("nom"));
              items.add(intent.getStringExtra("mail"));
              items.add(intent.getStringExtra("tel"));
           }
BSK-Team
  • 1,562
  • 17
  • 31
5
Intent i = new Intent(this, ActivityTwo.class);//this your current class
startActivity(i); 
to pass a value in class 1

Intent i = new Intent(this, ActivityTwo.class);
i.putExtra("Value1", "This value one for ActivityTwo ");
i.putExtra("Value2", "This value two ActivityTwo"); 
to get values .from ActivityTwo.class

Bundle extras = getIntent().getExtras();
if (extras == null) {
  return;
}
// get data via the key
String value1 = extras.getString(Intent.EXTRA_TEXT);
if (value1 != null) {
  // do something with the data
} 
Chetak Bhimani
  • 486
  • 4
  • 18
1

Generally you can pass data from one Activity to another with an Intent like this:

In your first Activity:

// Create your Intent
Intent intent = new Intent(context, TargetActivity.class);

// Now you can add extras to the intent, you identify extras with a String key
intent.putExtra("text", someString);
intent.putExtra("amount", someInteger);

// Then you start your Activity with this Intent
startActivity(intent);

In your second Activity:

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

    // Only get data from Intent when the Activity is new
    if(savedInstanceState == null) {
        Intent intent = getIntent();

        // Now you read the values from the Intent
        String someString = intent.getStringExtra("text");

        int someInteger = intent.getIntExtra("amount", 0);
    }
}
Xaver Kapeller
  • 46,604
  • 11
  • 87
  • 82
1

hey its simple look at the code

for sending

Intent i = new Intent(getApplicationContext(), Confirmation.class)  
i.putExtra("name",etName.getText().toString()));
i.putExtra("pass",etPass.getText().toString());
startActivity(i); 

for recieving in next activity

    Bundle extras = getIntent().getExtras();
                  String strEmployeeID="";
                  if (extras != null)
                  {

                      String value = extras.getString("name");
                              String value1 = extras.getString("pass");
    //                
Toast.makeText(getBaseContext(),  value, Toast.LENGTH_LONG).show();
                      strEmployeeID = value;
                               strEmployeePass = value1;
                  }
InnocentKiller
  • 5,167
  • 7
  • 32
  • 82
Tushar Narang
  • 1,772
  • 2
  • 17
  • 44
0

if you want to send data between two Activities .You must call following one,

 startActivityForResult(getcontext(), SecondActivity.class);

On calling this,Activity starts the second.In turn, second response to this intent and reply back with necessary data.

 public void startActivityForResult(Intent intent, int requestCode, Bundle options) {
        if (mParent == null) {
            Instrumentation.ActivityResult ar =
                mInstrumentation.execStartActivity(
                    this, mMainThread.getApplicationThread(), mToken, this,
                    intent, requestCode, options);
            if (ar != null) {
                mMainThread.sendActivityResult(
                    mToken, mEmbeddedID, requestCode, ar.getResultCode(),
                    ar.getResultData());
            }
            if (requestCode >= 0) {

                mStartedActivity = true;
            }
        } else {
            if (options != null) {
                mParent.startActivityFromChild(this, intent, requestCode, options);
            } else {
                // Note we want to go through this method for compatibility with
                // existing applications that may have overridden it.
                mParent.startActivityFromChild(this, intent, requestCode);
            }
        }
    }

Following one you must call this on the First Activity (from where the intent called).It helps to recieves the reply data from second Activity.

public void onActivityResult(int requestCode, int resultCode, Intent data) {

        if(!isFeatureInstalled(getContext()))
            return;

        if(requestCode == GET_REMINDER_DETAILS && resultCode == Activity.RESULT_OK)
        {
            mFilled = true;
            fillDataFromIntent(data);
            checkALertRequired.setChecked(true);
        }
    }
raguM.tech.
  • 183
  • 11
0

just simply do it like that:

passing data from 1st activity to 2nd activity:

Intent intent = new Intent(yourafirstctivity.this,Yoursecondactivity.class);
       intent.putExtra("yourkey", yourvalue);
       intent.putExtra("yourkey", yourvalue);
       intent.putExtra("yourkey", yourvalue);
       startActivity(intent);

and get the data from 1st activity to 2nd activity:

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.yourlayout);


String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");
String yourstring = getIntent().getExtras().getString("yourkey");

}
Farhan Shah
  • 2,244
  • 7
  • 23
  • 52