how to save intent data passed from 1st activity t

2019-06-11 16:03发布

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.

1条回答
劳资没心,怎么记你
2楼-- · 2019-06-11 16:38

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

查看更多
登录 后发表回答