【问题标题】:How to pass value from 1st activity to 3rd activity如何将价值从第一个活动传递到第三个活动
【发布时间】:2012-08-30 05:21:17
【问题描述】:

我希望将值从第一个活动传递到第三个活动。

我的第一个活动:CustomizedListview

第二个活动是:SingleMenuItemActivity

第三个活动是:InsertionExample

在这里,我必须将 Orderid 值从 CustomisedListView(第 1 个)活动传递给 InsertionExample(第 3 个)活动。

我怎样才能通过这个?我已将 orderid 值从第一个活动传递到第二个活动。但我不能将它从第一个活动传递到第三个活动。请帮帮我。

【问题讨论】:

  • 这不是共享偏好的目的

标签: android android-intent xml-parsing sharedpreferences


【解决方案1】:

试试这个

 Intent intent=new Intent(CustomizedListview.this,InsertionExample.class);
 intent.putExtra("orderid",getOrderid);
 startActivity(intent);

在你的第三个活动中

 Bundle bundle = data.getExtras();
 String getOrderId = bundle.getString("orderid");

【讨论】:

    【解决方案2】:

    我必须将 orderid 值从第一个活动传递到第二个活动

    发送到第二个活动时发送。只需将第二个活动的名称更改为第三个活动。

    或者

    只需在 Shared Preference 中存储 Order Id 并在第三个 Activity 中获取它。

    SharedPreference 示例

    SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    SharedPreferences.Editor prefsEditor = myPrefs.edit();
    prefsEditor.putString("order_id", "5");
    prefsEditor.commit();
    

    获取共享偏好。

     SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
     String prefName = myPrefs.getString("order_id", "0");
    

    【讨论】:

      【解决方案3】:

      您可以在第一个活动到第二个活动中使用额外的意图,然后从第二个到第三个的另一个意图中通过额外的额外值传递相同的值。

      【讨论】:

        【解决方案4】:

        您可以通过两种方式传递值:

        1. 要么创建一个全局类并在该类中设置值,然后 在您的第三个活动中访问该课程
        2. 您可以使用 Intent 将您的值从第一个活动发送到第二个活动

        Intent intent = new Intent (this, 2ndActivity.class); 
            intent.putExtra ("Value",Value);
            startActivity(intent);
        

        第二个活动到第三个活动也可以这样做

        Bundle extras = getIntent().getExtras();
            if(extras!=null){
            Values=extras.getString("value");
            }
        

        【讨论】:

          【解决方案5】:

          使用 SharedPreferences(用于小数据)来存储数据并在您 3 活动中获取此数据 否则使用内部存储或数据库(用于大数据)

          【讨论】:

            【解决方案6】:

            将值设为静态,然后在第三个活动中使用它

            public static int i;
            

            然后,在第三个活动中调用它:

            firstActivity.i;
            

            【讨论】:

            • 最好不要将其存储在静态值中,而是存储在单例中,例如扩展应用程序类并存储在哪里。
            • 如果您的应用在内存不足的情况下返回第三个活动,这将具有"" 的值,因此可能导致意外崩溃等(除非您将 saveData 持久保存到 BaseActivity.onSaveInstanceState 中的 Bundle并严格恢复一次,并且您的第三个 Activity 是 singleTask ,因此您无法更改由第三个 Activity 的多个实例读取的全局变量。)
            【解决方案7】:
            //your 1st activity (CustomizedListview)
                         
            String str=txtView.getText().toString();
            
            Intent i=new Intent(getApplicationContext(),SingleMenuItemActivity.class);
            i.putExtra("message",str);
            
            startActivity(i);
            
                //your 2nd Activity (SingleMenuItemActivity)
            
            
            String str= getIntent().getStringExtra("message");
            
            Intent i = new Intent(getApplicationContext(), InsertionExample.class);
            
            i.putExtra("message", str);
            
            startActivity(i);
            
                //you 3rd Activity (InsertionExample)
            
            
            Intent int=getIntent();
            
            String str= int.getStringExtra("message");
            
            txtView.setText(str);
            

            【讨论】:

              猜你喜欢
              • 2017-06-14
              • 2012-12-08
              • 1970-01-01
              • 2016-06-17
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2021-05-14
              • 1970-01-01
              相关资源
              最近更新 更多