简体   繁体   中英

how to save intent data passed from 1st activity to 2nd activity

I have Myactivity which is getting the intent data from the former activity. The intent data is sent through a spinner of the activity. I want to save this intent data of the spinner to the Myactvity. the intent data should persist when i enter the Myactivity through menu option of next activity.

in your First Activity1

Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
myintent.putExtra("Name", "your String");
startActivity(myintent);

in Second Activity2

Intent myintent = getIntent();

if(null!=myintent.getExtras()){
    String Name= myintent.getExtras().getString("Name");        
    Toast.makeText(getApplicationContext(),""+Name,12).show();                              
}else{
    Toast.makeText(getApplicationContext(),"No Recor Here..",12).show();
}

like SharedPreferences[2] in your First ActivityA

Intent myintent= new Intent(FirstActivity.this,SecondActivity.class);
SharedPreferences spref = this.getSharedPreferences("mynotifyid", MODE_WORLD_WRITEABLE);
SharedPreferences.Editor spreedit = spref.edit();
spreedit.putString("Name1", str1.toString());   
spreedit.putString("Name2", str2.toString());   
spreedit.putString("Name3", str3.toString());   
spreedit.putString("Name4", str4.toString());   
spreedit.commit();
startActivity(myintent);

in your Second ActivityB

SharedPreferences spref = context.getSharedPreferences("mynotifyid", Context.MODE_WORLD_WRITEABLE);
String str1 = spref.getString("Name1","");
String str2 = spref.getString("Name2","");
String str3 = spref.getString("Name3","");
String str4 = spref.getString("Name4","");

for your object saving purpose use SharedPreferences

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM