-3

NOTE: I know how to pass data Activity1 to Activity2 Using Bundle and PutExtra.

I refer this below links :

QUESTION

I've 3 activitys, Activity1, Activity2 and Activity3

In Activity 1 i have data. When i click on data Activity2 is started.

In Activity2 I've Textview. when i click on textView then open Activity3

In Activity3 i have EditText

So i want to pass data Activity1 TO Activity2 AND Activity2 TO Activity3

Activity1 code:

private OnItemClickListener listener = new OnItemClickListener() {
        @Override
        public void onItemClick(Base item) {
            Company company = (Company) item;
            Intent intent = new Intent(getActivity(), CompanyDetailActivity.class);
            //intent.putExtra("company", company);
            //intent.putExtra("edit", "editFrag");
            intent.putExtra("company", item);
            startActivity(intent);
            Log.e("Item...","Item clicked "+company.getName());
        }
    };

Activity2 code:

case R.id.rlCompanyProfile:
            //Base item = null;
            Company company = (Company) getIntent().getExtras().getSerializable("company") ;
            Intent intent = new Intent(this, AddCompanyActivity.class);
            intent.putExtra("company", company);
            //intent.putExtra("company", myData);
            intent.putExtra("edit", "editFrag");
            startActivity(intent);
            break;

Activity3 code:

Intent extras1 = getIntent();
        {


                        Company value = (Company) extras1.getSerializableExtra("company");
                        etCompanyName.setText(value.getName());
                        etWebsite.setText(value.getWebsite());
                        etEmail.setText(value.getEmail());
                        etPhoneHome.setText(value.getPhoneHome());
                        etPhonePrimary.setText(value.getPhonePrimary());
                        etAddressLine1.setText(value.getAddressLine1());
                        etAddressLine2.setText(value.getAddressLine2());
                        etCity.setText(value.getCity());
                        etZip.setText(value.getZipcode());
                    }
Ali
  • 3,066
  • 4
  • 15
  • 46

5 Answers5

2

The problem is in Here Activity2 code check it

You are passing item = null

Activity2 code:

case R.id.rlCompanyProfile:
Base item = null; // item object is null here
Company company = (Company) item ;// here you are passing null item object of your Company class
Intent intent = new Intent(this, AddCompanyActivity.class);
intent.putExtra("company", item);
intent.putExtra("edit", "editFrag");
startActivity(intent);
break;

EDIT

to send data to Activity use this

intent.putExtra("company", company);

To receive data in third activity use this

Company company=intent.getSerializableExtra("company");
AskNilesh
  • 58,437
  • 15
  • 99
  • 129
1

You can use intent or bundle to pass the data.

You can also use shared preference to store and retrieve data. Use intent as below :

// send data
    Intent intent = new Intent(Activity1.this, Activity2.class);
    intent.putExtra("detailData",data);
    startActivity(intent);
//retrieve data
    Intent intent = getIntent();
    String detailData = intent.getStringExtra("detailData");

Using bundle

 //Create the bundle 
 Bundle bundle = new Bundle();
//Add your data from getFactualResults method to bundle
bundle.putString("VENUE_NAME", venueName);
//Add the bundle to the intent
i.putExtras(bundle);
startActivity(i);
//retrie bundle 
Bundle bundle = getIntent().getExtras();

//Extract the data…
String venName = bundle.getString("VENUE_NAME");

Shared Prefrence :

SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode
Editor editor = pref.edit();
    SharedPreferences.Editor editor = sharedpreferences.edit();
                editor.putString("key", value);
                editor.commit();
//retrieve data
pref.getString("key", null); // getting String
Rozina
  • 349
  • 2
  • 14
0

You can achieve this nature using startActivityForResult() method of Activity class.

Refer this link startActivityForResult

buzzingsilently
  • 1,375
  • 1
  • 10
  • 17
0

Is not that hard, just use putExtra() in order to pass data between activities, the workflow will be this, first use putExtra() as your first intent to go to the second Activity, after your reach Activity 2, just get the results with the bundle getExtras() and after that just create another intent.putExtra() to send that data to Activity 3

The workflow should be like this

Activity 1

String myData = "StackOverflowRocks";

Intent intent = new Intent(getBaseContext(), Activity2.class);
intent.putExtra("THE_ID_OF_WHAT_YOU_ARE_SENDING", myData);
startActivity(intent);

At Activity 2 just get that data

 Bundle extras = getIntent().getExtras(); 
 String getData;

if (extras != null) {
    userName = extras.getString("THE_ID_OF_WHAT_YOU_ARE_SENDING");
    // and get whatever type user account id is
}

Now that you have the data in Activity 2 , do the same thing as Activity 1 to Activity 2

String myData = getData;

Intent intent = new Intent(getBaseContext(), Activity3.class);
intent.putExtra("THE_ID_OF_WHAT_YOU_ARE_SENDING2", myData);
startActivity(intent);

And get it in your Activity 3

You can check your extras this way.

Bundle extras = getIntent().getExtras(); 
 String getData;

if (extras != null) {
    userName = extras.getString("THE_ID_OF_WHAT_YOU_ARE_SENDING2");
    // and get whatever type user account id is
}

Edit: your problem is that you are sending data to your Activity 2 as item1 , but then in Activity 2 you are not getting that item1 data, you are just creating a new one and sending it to Activity 3.

Check this in your Activity 2

Intent intent = new Intent(this, AddCompanyActivity.class);
intent.putExtra("company", item1);
intent.putExtra("edit", "editFrag");

Instead of creating a new item1, just get the data from the item1 from your activity 1:

String company = getIntent().getStringExtra("company");
halfer
  • 18,701
  • 13
  • 79
  • 158
Gastón Saillén
  • 9,076
  • 4
  • 39
  • 58
0

You can use sharedpreferance to store the data of Activity1 and Activity2 to pass in Activity3.

pravin maske
  • 83
  • 1
  • 12