【问题标题】:how to save intent data passed from 1st activity to 2nd activity如何保存从第一个活动传递到第二个活动的意图数据
【发布时间】:2012-09-14 13:56:24
【问题描述】:

我有 Myactivity,它正在从前一个活动中获取意图数据。意图数据通过活动的微调器发送。我想将微调器的这个意图数据保存到 Myactvity。当我通过下一个活动的菜单选项进入 Myactivity 时,意图数据应该保持不变。

【问题讨论】:

    标签: android database android-intent sharedpreferences android-sqlite


    【解决方案1】:

    在你的第一个活动1中

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

    在第二个活动2中

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

    喜欢 SharedPreferences[2] 在您的第一个 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);
    

    在您的第二个活动中B

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

    为了您的对象保存目的,使用 SharedPreferences

    【讨论】:

    • 代码可以使用sharedpreference吗?如何使用 sharedpreference 来保存 Intent 数据,而不是 toast?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 1970-01-01
    • 2020-01-13
    • 2012-12-26
    • 2019-12-31
    相关资源
    最近更新 更多